Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / DnsCache.cs / 1 / DnsCache.cs
//---------------------------------------------------------------------------- // Copyright (c) Microsoft Corporation. All rights reserved. //--------------------------------------------------------------------------- namespace System.ServiceModel.Channels { using System.Collections.Generic; using System.Diagnostics; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Security.Permissions; using System.ServiceModel; using System.ServiceModel.Dispatcher; using System.Text; static class DnsCache { const int mruWatermark = 64; static MruCacheresolveCache = new MruCache (mruWatermark); static readonly TimeSpan cacheTimeout = TimeSpan.FromSeconds(2); static string machineName; static object ThisLock { get { return resolveCache; } } public static string MachineName { get { if (machineName == null) { lock (ThisLock) { if (machineName == null) { try { machineName = Dns.GetHostEntry(String.Empty).HostName; } catch (SocketException exception) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(exception, TraceEventType.Information); } // we fall back to the NetBios machine if Dns fails machineName = UnsafeNativeMethods.GetComputerName(ComputerNameFormat.PhysicalNetBIOS); } } } } return machineName; } } public static IPHostEntry Resolve(string hostName) { IPHostEntry hostEntry = null; DateTime now = DateTime.UtcNow; lock (ThisLock) { DnsCacheEntry cacheEntry; if (resolveCache.TryGetValue(hostName, out cacheEntry)) { if (now.Subtract(cacheEntry.TimeStamp) > cacheTimeout) { resolveCache.Remove(hostName); } else { if (cacheEntry.HostEntry == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new EndpointNotFoundException(SR.GetString(SR.DnsResolveFailed, hostName))); } hostEntry = cacheEntry.HostEntry; } } } if (hostEntry == null) { SocketException dnsException = null; try { hostEntry = Dns.GetHostEntry(hostName); } catch (SocketException e) { dnsException = e; } lock (ThisLock) { // MruCache doesn't have a this[] operator, so we first remove (just in case it exists already) resolveCache.Remove(hostName); resolveCache.Add(hostName, new DnsCacheEntry(hostEntry, now)); } if (dnsException != null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( new EndpointNotFoundException(SR.GetString(SR.DnsResolveFailed, hostName), dnsException)); } } return hostEntry; } class DnsCacheEntry { IPHostEntry hostEntry; DateTime timeStamp; public DnsCacheEntry(IPHostEntry hostEntry, DateTime timeStamp) { this.hostEntry = hostEntry; this.timeStamp = timeStamp; } public IPHostEntry HostEntry { get { return hostEntry; } } public DateTime TimeStamp { get { return timeStamp; } } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- MatrixIndependentAnimationStorage.cs
- HtmlControl.cs
- CmsInterop.cs
- KeyboardEventArgs.cs
- UInt16Converter.cs
- ModuleBuilder.cs
- PageVisual.cs
- SoapElementAttribute.cs
- TrustSection.cs
- TextElementCollection.cs
- XmlSiteMapProvider.cs
- CellPartitioner.cs
- ConfigurationManagerHelper.cs
- FrameworkElement.cs
- LockedHandleGlyph.cs
- DbParameterCollectionHelper.cs
- EdmTypeAttribute.cs
- DecimalAnimationBase.cs
- CommandBindingCollection.cs
- XamlVector3DCollectionSerializer.cs
- AspNetPartialTrustHelpers.cs
- WorkflowDefinitionContext.cs
- EndpointConfigContainer.cs
- ToolStripItemImageRenderEventArgs.cs
- FreezableDefaultValueFactory.cs
- EventDriven.cs
- DataGridViewRowErrorTextNeededEventArgs.cs
- HttpResponseBase.cs
- xsdvalidator.cs
- WebPartCloseVerb.cs
- ComponentEditorForm.cs
- AbsoluteQuery.cs
- AnimationClock.cs
- TextContainerChangeEventArgs.cs
- DrawingContextDrawingContextWalker.cs
- RadioButton.cs
- XPathMultyIterator.cs
- JsonQueryStringConverter.cs
- DesignTimeParseData.cs
- ThumbAutomationPeer.cs
- ContentPlaceHolder.cs
- ThicknessKeyFrameCollection.cs
- LogEntryHeaderDeserializer.cs
- QilStrConcat.cs
- ObjectAnimationUsingKeyFrames.cs
- PublishLicense.cs
- SecurityVersion.cs
- HtmlTableRow.cs
- SimpleHandlerBuildProvider.cs
- PerspectiveCamera.cs
- TreeSet.cs
- BadImageFormatException.cs
- DoubleLink.cs
- DependencyProperty.cs
- HttpContext.cs
- ErrorFormatter.cs
- ExternalFile.cs
- EmbossBitmapEffect.cs
- BulletChrome.cs
- MatrixKeyFrameCollection.cs
- NameNode.cs
- DependencySource.cs
- ConfigXmlAttribute.cs
- DeviceFilterDictionary.cs
- datacache.cs
- Composition.cs
- CheckBoxList.cs
- SessionSwitchEventArgs.cs
- BamlLocalizabilityResolver.cs
- FilterableAttribute.cs
- MasterPage.cs
- FunctionParameter.cs
- GPRECTF.cs
- MobileUserControlDesigner.cs
- Geometry.cs
- InlineCollection.cs
- BooleanKeyFrameCollection.cs
- coordinatorscratchpad.cs
- SQLInt64Storage.cs
- SolidColorBrush.cs
- NetPeerTcpBindingCollectionElement.cs
- BoolExpressionVisitors.cs
- Error.cs
- DataChangedEventManager.cs
- SchemaTypeEmitter.cs
- AmbientLight.cs
- EventRouteFactory.cs
- BaseTemplateCodeDomTreeGenerator.cs
- HttpRequestTraceRecord.cs
- ClearCollection.cs
- DetailsViewInsertedEventArgs.cs
- AssemblyCacheEntry.cs
- DataControlPagerLinkButton.cs
- TransactionsSectionGroup.cs
- ITreeGenerator.cs
- XhtmlCssHandler.cs
- ClipboardProcessor.cs
- StretchValidation.cs
- StickyNoteAnnotations.cs
- PopupRoot.cs