private static void OnCloseComplete(IAsyncResult result) { if (result.CompletedSynchronously) { return; } RefcountedCommunicationObject.CloseAsyncResult asyncState = (RefcountedCommunicationObject.CloseAsyncResult)result.AsyncState; using (Activity activity = ServiceModelActivity.BoundOperation(null)) { Exception exception = null; try { asyncState.HandleCloseComplete(result); } catch (Exception exception2) { Exception exception1 = exception2; if (Fx.IsFatal(exception1)) { throw; } exception = exception1; if (Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldTraceWarning) { DiagnosticTrace diagnosticTrace = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.DiagnosticTrace; string traceCodeCommunicationObjectCloseFailed = Resources.TraceCodeCommunicationObjectCloseFailed; object[] str = new object[] { asyncState.communicationObject.GetCommunicationObjectType().ToString() }; diagnosticTrace.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectCloseFailed, Microsoft.ServiceBus.SR.GetString(traceCodeCommunicationObjectCloseFailed, str), null, null, asyncState); } asyncState.communicationObject.Abort(); } asyncState.Complete(false, exception); } }
public IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state) { CommunicationState communicationState; IAsyncResult skippingOperationAsyncResult; ServiceModelActivity serviceModelActivity; if (timeout < TimeSpan.Zero) { throw Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("timeout", Microsoft.ServiceBus.SR.GetString(Resources.SFxTimeoutOutOfRange0, new object[0]))); } if (!Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldUseActivity || !this.TraceOpenAndClose) { serviceModelActivity = null; } else { serviceModelActivity = this.CreateCloseActivity(); } using (serviceModelActivity) { lock (this.ThisLock) { if (this.refCount == 0) { throw new InvalidOperationException(SRClient.InvalidRefcountedCommunicationObject); } RefcountedCommunicationObject refcountedCommunicationObject = this; int num = refcountedCommunicationObject.refCount - 1; int num1 = num; refcountedCommunicationObject.refCount = num; if (num1 <= 0) { communicationState = this.state; if (communicationState != CommunicationState.Closed) { this.state = CommunicationState.Closing; } this.closeCalled = true; } else { skippingOperationAsyncResult = new RefcountedCommunicationObject.SkippingOperationAsyncResult(callback, state); return(skippingOperationAsyncResult); } } switch (communicationState) { case CommunicationState.Created: case CommunicationState.Opening: case CommunicationState.Faulted: { this.Abort(); if (communicationState == CommunicationState.Faulted) { throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this); } skippingOperationAsyncResult = new RefcountedCommunicationObject.AlreadyClosedAsyncResult(callback, state); return(skippingOperationAsyncResult); } case CommunicationState.Opened: { bool flag = true; try { this.OnClosing(); if (!this.onClosingCalled) { throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, this); } IAsyncResult closeAsyncResult = new RefcountedCommunicationObject.CloseAsyncResult(this, timeout, callback, state); flag = false; skippingOperationAsyncResult = closeAsyncResult; return(skippingOperationAsyncResult); } finally { if (flag) { if (Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldTraceWarning) { DiagnosticTrace diagnosticTrace = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.DiagnosticTrace; string traceCodeCommunicationObjectCloseFailed = Resources.TraceCodeCommunicationObjectCloseFailed; object[] str = new object[] { this.GetCommunicationObjectType().ToString() }; diagnosticTrace.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectCloseFailed, Microsoft.ServiceBus.SR.GetString(traceCodeCommunicationObjectCloseFailed, str), null, null, this); } this.Abort(); } } break; } case CommunicationState.Closing: case CommunicationState.Closed: { skippingOperationAsyncResult = new RefcountedCommunicationObject.AlreadyClosedAsyncResult(callback, state); return(skippingOperationAsyncResult); } } throw Fx.AssertAndThrow("CommunicationObject.BeginClose: Unknown CommunicationState"); } return(skippingOperationAsyncResult); }