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
- SQLStringStorage.cs
- DescendentsWalkerBase.cs
- AccessDataSourceWizardForm.cs
- SafeBuffer.cs
- TableRow.cs
- DataGridRelationshipRow.cs
- Literal.cs
- QilReplaceVisitor.cs
- ToolStripLabel.cs
- CardSpacePolicyElement.cs
- StatusBarItem.cs
- CodeNamespace.cs
- _TimerThread.cs
- ExpressionBuilder.cs
- DefaultPropertyAttribute.cs
- FragmentNavigationEventArgs.cs
- MultiplexingDispatchMessageFormatter.cs
- MsdtcClusterUtils.cs
- LinqDataSourceContextData.cs
- MultiView.cs
- AdornerHitTestResult.cs
- CommandField.cs
- TypeBuilder.cs
- ExceptionRoutedEventArgs.cs
- Int32KeyFrameCollection.cs
- PngBitmapEncoder.cs
- ProviderBase.cs
- ProviderConnectionPoint.cs
- DBSqlParserColumnCollection.cs
- MulticastDelegate.cs
- SQLString.cs
- Serializer.cs
- ImageSource.cs
- XmlNavigatorStack.cs
- Object.cs
- SchemaAttDef.cs
- SchemaTypeEmitter.cs
- CompilerErrorCollection.cs
- ParameterCollection.cs
- SiteMap.cs
- TextRunProperties.cs
- GridView.cs
- TrackingRecord.cs
- XmlQueryContext.cs
- CapabilitiesPattern.cs
- OdbcErrorCollection.cs
- BindableTemplateBuilder.cs
- PathSegmentCollection.cs
- streamingZipPartStream.cs
- PagerSettings.cs
- InternalEnumValidatorAttribute.cs
- ImageInfo.cs
- TypeListConverter.cs
- DataBindingValueUIHandler.cs
- TextTreeInsertUndoUnit.cs
- SmtpFailedRecipientException.cs
- NameTable.cs
- EmbeddedMailObject.cs
- Authorization.cs
- VectorAnimation.cs
- ConstructorArgumentAttribute.cs
- CodeCommentStatementCollection.cs
- BitmapPalette.cs
- PathNode.cs
- XmlUrlEditor.cs
- BitmapInitialize.cs
- XmlBinaryReader.cs
- TitleStyle.cs
- SpellerStatusTable.cs
- TypeResolvingOptionsAttribute.cs
- FactoryGenerator.cs
- SemanticValue.cs
- SafeFindHandle.cs
- ThicknessAnimationUsingKeyFrames.cs
- EntityDataSourceMemberPath.cs
- ToolStrip.cs
- ScriptReferenceEventArgs.cs
- ExpandedProjectionNode.cs
- HebrewCalendar.cs
- Accessors.cs
- SqlConnectionPoolProviderInfo.cs
- DragDrop.cs
- NotImplementedException.cs
- WebExceptionStatus.cs
- DriveInfo.cs
- UserValidatedEventArgs.cs
- Documentation.cs
- AsymmetricKeyExchangeDeformatter.cs
- Dynamic.cs
- GridEntryCollection.cs
- ControlBindingsCollection.cs
- MarkupExtensionSerializer.cs
- ProtocolsSection.cs
- StringHandle.cs
- cache.cs
- MultiView.cs
- Decorator.cs
- ValidatedControlConverter.cs
- PerformanceCounter.cs
- ProgressChangedEventArgs.cs