ICommunicationObjectExtensions

Coordinator
Dec 23, 2011 at 6:20 PM

using System;
using System.ServiceModel;

namespace Fox.Silverlight.Extensions
{
    /// <summary>
    /// Extensions for <see cref="System.ServiceModel.ICommunicationObject"/>.
    /// </summary>
    public static class ICommunicationObjectExtensions
    {
        /// <summary>
        /// Closes the connection.
        /// </summary>
        /// <param name="serviceClient">The service client.</param>
        /// <remarks>
        /// For more detail see �IDisposable and WCF�
        /// by Steve Smith
        /// [http://stevesmithblog.com/blog/idisposable-and-wcf/]
        /// </remarks>
        public static void CloseConnection(this ICommunicationObject serviceClient)
        {
            if (serviceClient.State != CommunicationState.Opened) return;
            try
            {
                serviceClient.Close();
            }
            catch (CommunicationException ex)
            {
                serviceClient.Abort();
            }
            catch (TimeoutException ex)
            {
                serviceClient.Abort();
            }
            catch (Exception ex)
            {
                serviceClient.Abort();
                throw;
            }
        }

        /// <summary>
        /// Closes the connection or abort.
        /// </summary>
        /// <param name="serviceClient">The service client.</param>
        public static void CloseConnectionOrAbort(this ICommunicationObject serviceClient)
        {
            serviceClient.CloseConnection();
            if (serviceClient == null) return;
            if (serviceClient.State != CommunicationState.Closed)
            {
                serviceClient.Abort();
            }
        }

        /// <summary>
        /// Determines whether the specified service client is opened.
        /// </summary>
        /// <param name="serviceClient">The service client.</param>
        /// <returns>
        ///   <c>true</c> if the specified service client is opened; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsOpened(this ICommunicationObject serviceClient)
        {
            return (serviceClient.State == CommunicationState.Opened);
        }
    }
}

Coordinator
Feb 2, 2012 at 5:16 PM

Hey, me! Here's a fix for CloseConnectionOrAbort():

 

using System;
using System.ServiceModel;
using System.Threading;

namespace Fox.Extensions
{
    /// <summary>
    /// Extensions for <see cref="System.ServiceModel.ICommunicationObject"/>.
    /// </summary>
    public static class ICommunicationObjectExtensions
    {
        /// <summary>
        /// Closes the connection.
        /// </summary>
        /// <param name="serviceClient">The service client.</param>
        /// <remarks>
        /// For more detail see �IDisposable and WCF�
        /// by Steve Smith
        /// [http://stevesmithblog.com/blog/idisposable-and-wcf/]
        /// </remarks>
        public static void CloseConnection(this ICommunicationObject serviceClient)
        {
            if (serviceClient == null) return;
            if (serviceClient.State != CommunicationState.Opened) return;

            try
            {
                serviceClient.Close();
            }
            catch (CommunicationException ex)
            {
                serviceClient.Abort();
            }
            catch (TimeoutException ex)
            {
                serviceClient.Abort();
            }
            catch (Exception ex)
            {
                serviceClient.Abort();
                throw;
            }
        }

        /// <summary>
        /// Closes the connection or abort.
        /// </summary>
        /// <param name="serviceClient">The service client.</param>
        /// <param name="waitInMilliseconds">The wait in milliseconds.</param>
        public static void CloseConnectionOrAbort(this ICommunicationObject serviceClient, int waitInMilliseconds = 500)
        {
            if (serviceClient == null) return;
            serviceClient.CloseConnection();

            Thread.Sleep(waitInMilliseconds);

            if ((serviceClient.State != CommunicationState.Closed) &&
                (serviceClient.State != CommunicationState.Closing))
            {
                serviceClient.Abort();
            }
        }

        /// <summary>
        /// Determines whether the specified service client is opened.
        /// </summary>
        /// <param name="serviceClient">The service client.</param>
        /// <returns>
        ///   <c>true</c> if the specified service client is opened; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsOpened(this ICommunicationObject serviceClient)
        {
            return (serviceClient.State == CommunicationState.Opened);
        }
    }
}