Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / ChannelTracker.cs / 1 / ChannelTracker.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.ServiceModel.Channels { using System.Collections.Generic; using System.ServiceModel; using System.Diagnostics; using System.IO; using System.Runtime.Serialization; using System.ServiceModel.Diagnostics; using System.Text; using System.Threading; using System.ServiceModel.Description; using System.Xml; ////// Track channels and (optionally) associated state /// class ChannelTracker: CommunicationObject where TChannel : IChannel where TState : class { Dictionary receivers; EventHandler onInnerChannelClosed; EventHandler onInnerChannelFaulted; public ChannelTracker() { this.receivers = new Dictionary (); this.onInnerChannelClosed = new EventHandler(OnInnerChannelClosed); this.onInnerChannelFaulted = new EventHandler(OnInnerChannelFaulted); } public void Add(TChannel channel, TState channelReceiver) { bool abortChannel = false; lock (this.receivers) { if (this.State != CommunicationState.Opened) { abortChannel = true; } else { this.receivers.Add(channel, channelReceiver); } } if (abortChannel) { channel.Abort(); } } public void PrepareChannel(TChannel channel) { channel.Faulted += this.onInnerChannelFaulted; channel.Closed += this.onInnerChannelClosed; } void OnInnerChannelFaulted(object sender, EventArgs e) { ((TChannel)sender).Abort(); } void OnInnerChannelClosed(object sender, EventArgs e) { // remove the channel from our tracking dictionary TChannel channel = (TChannel)sender; this.Remove(channel); channel.Faulted -= this.onInnerChannelFaulted; channel.Closed -= this.onInnerChannelClosed; } public bool Remove(TChannel channel) { lock (this.receivers) { return this.receivers.Remove(channel); } } TChannel[] GetChannels() { lock (this.receivers) { TChannel[] channels = new TChannel[this.receivers.Keys.Count]; this.receivers.Keys.CopyTo(channels, 0); this.receivers.Clear(); return channels; } } protected override void OnAbort() { TChannel[] channels = GetChannels(); for (int i = 0; i < channels.Length; i++) { channels[i].Abort(); } } protected override void OnClose(TimeSpan timeout) { TimeoutHelper timeoutHelper = new TimeoutHelper(timeout); TChannel[] channels = GetChannels(); for (int i = 0; i < channels.Length; i++) { bool success = false; try { channels[i].Close(timeoutHelper.RemainingTime()); success = true; } catch (CommunicationException e) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Information); } } catch (TimeoutException e) { if (DiagnosticUtility.ShouldTraceInformation) { DiagnosticUtility.ExceptionUtility.TraceHandledException(e, TraceEventType.Information); } } finally { if (!success) { channels[i].Abort(); } } } } protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state) { TChannel[] channels = GetChannels(); List collection = new List (); for (int i = 0; i < channels.Length; i++) { collection.Add(channels[i]); } return new CloseCollectionAsyncResult(timeout, callback, state, collection); } protected override void OnEndClose(IAsyncResult result) { CloseCollectionAsyncResult.End(result); } protected override TimeSpan DefaultCloseTimeout { get { return ServiceDefaults.CloseTimeout; } } protected override TimeSpan DefaultOpenTimeout { get { return ServiceDefaults.OpenTimeout; } } protected override void OnOpen(TimeSpan timeout) { } protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state) { return new CompletedAsyncResult(callback, state); } protected override void OnEndOpen(IAsyncResult result) { CompletedAsyncResult.End(result); } } } // 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
- Bidi.cs
- ResourceManager.cs
- DecimalFormatter.cs
- SafeProcessHandle.cs
- DrawingVisual.cs
- OperationCanceledException.cs
- WebSysDescriptionAttribute.cs
- LineInfo.cs
- UInt64Storage.cs
- OperatingSystem.cs
- PublishLicense.cs
- CompressStream.cs
- X509SecurityTokenProvider.cs
- CacheModeValueSerializer.cs
- Point4DValueSerializer.cs
- ConfigurationPropertyCollection.cs
- DesignerLoader.cs
- HostingEnvironmentSection.cs
- ParseElement.cs
- RootBuilder.cs
- FieldCollectionEditor.cs
- QilTargetType.cs
- SafeSecurityHandles.cs
- exports.cs
- BuildProvider.cs
- DeflateEmulationStream.cs
- ElementAction.cs
- XmlRawWriterWrapper.cs
- ToolStripMenuItemCodeDomSerializer.cs
- ObjectViewFactory.cs
- StyleCollection.cs
- ClientSettings.cs
- Cursor.cs
- HttpCookieCollection.cs
- SingleSelectRootGridEntry.cs
- TransactionException.cs
- TextViewSelectionProcessor.cs
- UInt16Converter.cs
- DbExpressionVisitor.cs
- ExitEventArgs.cs
- Timer.cs
- BookmarkEventArgs.cs
- entitydatasourceentitysetnameconverter.cs
- SqlDataSourceFilteringEventArgs.cs
- MemberAssignment.cs
- TransportOutputChannel.cs
- XmlMtomReader.cs
- VectorCollectionConverter.cs
- EncryptedKey.cs
- CryptoHelper.cs
- DbExpressionVisitor_TResultType.cs
- AxImporter.cs
- UnsafeNativeMethods.cs
- DataRecordInfo.cs
- EditorZone.cs
- UserMapPath.cs
- FormViewUpdatedEventArgs.cs
- RtfToken.cs
- WorkflowRuntimeServiceElement.cs
- EndEvent.cs
- GuidConverter.cs
- PlatformNotSupportedException.cs
- DispatcherExceptionFilterEventArgs.cs
- DataGrid.cs
- SerializationObjectManager.cs
- BitmapEffectDrawingContent.cs
- BrushValueSerializer.cs
- SQLSingleStorage.cs
- ProxyManager.cs
- Vector3D.cs
- UnsafeNativeMethods.cs
- HttpException.cs
- AssemblyName.cs
- TraceInternal.cs
- PermissionSet.cs
- DescendantBaseQuery.cs
- HtmlSelect.cs
- ConstraintEnumerator.cs
- TransformPattern.cs
- Timer.cs
- _KerberosClient.cs
- DataBinder.cs
- UriSection.cs
- ProxyWebPart.cs
- DatePickerTextBox.cs
- SafeCryptContextHandle.cs
- IIS7UserPrincipal.cs
- ReverseInheritProperty.cs
- MSHTMLHostUtil.cs
- TextEndOfSegment.cs
- MdiWindowListItemConverter.cs
- ADMembershipProvider.cs
- EndpointDispatcher.cs
- SyntaxCheck.cs
- TextSpan.cs
- GroupByExpressionRewriter.cs
- XdrBuilder.cs
- XmlSchemaInferenceException.cs
- Literal.cs
- TableLayoutSettingsTypeConverter.cs