public static void SafeClose(this ICommunicationObject communicationObject, TimeSpan?timeout = null)
 {
     try
     {
         if (communicationObject != null && communicationObject.State != CommunicationState.Closed)
         {
             if (MessagingUtilities.safeCloseCommunicationObjectCallback == null)
             {
                 MessagingUtilities.safeCloseCommunicationObjectCallback = new AsyncCallback(MessagingUtilities.SafeCloseCommunicationObjectCallback);
             }
             if (!timeout.HasValue)
             {
                 communicationObject.BeginClose(MessagingUtilities.safeCloseCommunicationObjectCallback, communicationObject);
             }
             else
             {
                 communicationObject.BeginClose(timeout.Value, MessagingUtilities.safeCloseCommunicationObjectCallback, communicationObject);
             }
         }
     }
     catch (Exception exception1)
     {
         Exception exception = exception1;
         if (Fx.IsFatal(exception))
         {
             throw;
         }
         Fx.Exception.TraceHandled(exception, "MessagingUtilities.SafeClose(CommunicationObject)", null);
         if (communicationObject != null)
         {
             communicationObject.Abort();
         }
     }
 }
Beispiel #2
0
            public CloseAsyncResult(
                ICommunicationObject communicationObject,
                bool sharedInnerListener,
                TimeSpan timeout,
                AsyncCallback callback,
                object state
                ) : base(callback, state)
            {
                _communicationObject = communicationObject;

                if (_communicationObject == null || sharedInnerListener)
                {
                    Complete(true);
                    return;
                }

                IAsyncResult result = _communicationObject.BeginClose(
                    timeout,
                    _onCloseComplete,
                    this
                    );

                if (result.CompletedSynchronously)
                {
                    _communicationObject.EndClose(result);
                    Complete(true);
                }
            }
Beispiel #3
0
 // Helper used to close another CommunicationObject "owned" by the current one.
 // It is used to propagate the use of either the synchronous or asynchronous methods
 internal async Task CloseOtherAsync(ICommunicationObject other, TimeSpan timeout)
 {
     // If the current object is being closed synchronously, use the synchronous
     // close path for the other object.
     if (_isSynchronousClose)
     {
         await TaskHelpers.CallActionAsync(other.Close, timeout);
     }
     else
     {
         await Task.Factory.FromAsync(other.BeginClose(timeout, callback: null, state: null), other.EndClose);
     }
 }
 /// <summary>
 /// Desliga a comunicação.
 /// </summary>
 /// <param name="communicationObject"></param>
 public static void Shutdown(ICommunicationObject communicationObject)
 {
     if (communicationObject != null)
     {
         try
         {
             communicationObject.BeginClose(new AsyncCallback(CommunicationHelpers.ShutdownCallback), communicationObject);
         }
         catch (TimeoutException)
         {
             communicationObject.Abort();
         }
         catch (CommunicationException)
         {
             communicationObject.Abort();
         }
     }
 }
Beispiel #5
0
 /// <summary>
 /// Desliga a comunicação com o objeto informado.
 /// </summary>
 /// <param name="communicationObject"></param>
 private void ShutdownCommunicationObject(ICommunicationObject communicationObject)
 {
     if (communicationObject != null)
     {
         try
         {
             communicationObject.BeginClose(new AsyncCallback(ShutdownCommunicationObjectCallback), communicationObject);
         }
         catch (System.TimeoutException)
         {
             communicationObject.Abort();
         }
         catch (CommunicationException)
         {
             communicationObject.Abort();
         }
         catch (NullReferenceException)
         {
             communicationObject.Abort();
         }
     }
 }
Beispiel #6
0
        /// <summary>
        /// Helper class to async close ICommunicationObject
        /// </summary>
        /// <param name="obj">indicating the object</param>
        public static void AsyncCloseICommunicationObject(ICommunicationObject obj)
        {
            try
            {
                if (obj == null)
                {
                    return;
                }

                if (obj.State == CommunicationState.Faulted)
                {
                    obj.Abort();
                }
                else
                {
                    obj.BeginClose(callbackToCloseChannel, obj);
                }
            }
            catch (Exception)
            {
                obj.Abort();
            }
        }
 protected virtual IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
 {
     return(inner.BeginClose(timeout, callback, state));
 }
Beispiel #8
0
 // Helper used to close another CommunicationObject "owned" by the current one.
 // It is used to propagate the use of either the synchronous or asynchronous methods
 internal async Task CloseOtherAsync(ICommunicationObject other, TimeSpan timeout)
 {
     // If the current object is being closed synchronously, use the synchronous
     // close path for the other object.
     if (_isSynchronousClose)
     {
         await TaskHelpers.CallActionAsync<TimeSpan>(other.Close, timeout);
     }
     else
     {
         await Task.Factory.FromAsync(other.BeginClose(timeout, callback: null, state: null), other.EndClose);
     }
 }
 public System.IAsyncResult BeginClose(System.TimeSpan timeout, System.AsyncCallback callback, object state)
 {
     return(_communicationObject.BeginClose(timeout, callback, state));
 }
Beispiel #10
0
        /// <summary>
        /// Asynchronously closes the connection of the proxy.
        /// </summary>
        /// <exception cref="ArgumentNullException">The <paramref name="communicationObject"/> is null.</exception>
        public static Task CloseAsync(this ICommunicationObject communicationObject)
        {
            _ = communicationObject ?? throw new ArgumentNullException(nameof(communicationObject));

            return(Task.Factory.FromAsync(communicationObject.BeginClose(null, null), communicationObject.EndClose));
        }