public CloseAsyncResult(RefcountedCommunicationObject communicationObject, TimeSpan timeout, AsyncCallback callback, object state) : base(callback, state)
            {
                this.communicationObject = communicationObject;
                IAsyncResult asyncResult = this.communicationObject.OnBeginClose(timeout, RefcountedCommunicationObject.CloseAsyncResult.onCloseComplete, this);

                if (asyncResult.CompletedSynchronously)
                {
                    this.HandleCloseComplete(asyncResult);
                    base.Complete(true);
                }
            }
        public bool AddRef()
        {
            bool flag;

            lock (this.ThisLock)
            {
                if (this.state == CommunicationState.Closing || this.state == CommunicationState.Closed)
                {
                    flag = false;
                }
                else
                {
                    RefcountedCommunicationObject refcountedCommunicationObject = this;
                    refcountedCommunicationObject.refCount = refcountedCommunicationObject.refCount + 1;
                    flag = true;
                }
            }
            return(flag);
        }
        public void Close(TimeSpan timeout)
        {
            CommunicationState   communicationState;
            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
                    {
                        return;
                    }
                }
                switch (communicationState)
                {
                case CommunicationState.Created:
                case CommunicationState.Opening:
                case CommunicationState.Faulted:
                {
                    this.Abort();
                    if (communicationState != CommunicationState.Faulted)
                    {
                        return;
                    }
                    throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
                }

                case CommunicationState.Opened:
                {
                    bool flag = true;
                    try
                    {
                        this.OnClosing();
                        if (!this.onClosingCalled)
                        {
                            throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, this);
                        }
                        this.OnClose(timeout);
                        this.OnClosed();
                        if (!this.onClosedCalled)
                        {
                            throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosed"), Guid.Empty, this);
                        }
                        flag = false;
                        return;
                    }
                    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:
                {
                    return;
                }
                }
                throw Fx.AssertAndThrow("CommunicationObject.BeginClose: Unknown CommunicationState");
            }
        }
        public void Abort()
        {
            lock (this.ThisLock)
            {
                if (!this.closeCalled)
                {
                    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)
                    {
                        return;
                    }
                }
                if (this.aborted || this.state == CommunicationState.Closed)
                {
                    return;
                }
                else
                {
                    this.aborted = true;
                    this.state   = CommunicationState.Closing;
                }
            }
            if (Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldTraceInformation)
            {
                DiagnosticTrace diagnosticTrace = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.DiagnosticTrace;
                string          traceCodeCommunicationObjectAborted = Resources.TraceCodeCommunicationObjectAborted;
                object[]        objArray = new object[] { DiagnosticTrace.CreateSourceString(this) };
                diagnosticTrace.TraceEvent(TraceEventType.Information, TraceCode.CommunicationObjectAborted, Microsoft.ServiceBus.SR.GetString(traceCodeCommunicationObjectAborted, objArray), null, null, this);
            }
            bool flag = true;

            try
            {
                this.OnClosing();
                if (!this.onClosingCalled)
                {
                    throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, this);
                }
                this.OnAbort();
                this.OnClosed();
                if (!this.onClosedCalled)
                {
                    throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosed"), Guid.Empty, this);
                }
                flag = false;
            }
            finally
            {
                if (flag && Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldTraceWarning)
                {
                    DiagnosticTrace diagnosticTrace1 = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.DiagnosticTrace;
                    string          traceCodeCommunicationObjectAbortFailed = Resources.TraceCodeCommunicationObjectAbortFailed;
                    object[]        str = new object[] { this.GetCommunicationObjectType().ToString() };
                    diagnosticTrace1.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectAbortFailed, Microsoft.ServiceBus.SR.GetString(traceCodeCommunicationObjectAbortFailed, str), null, null, this);
                }
            }
        }