Return to Snippet

Revision: 55367
at February 3, 2012 23:53 by rstokoe


Initial Code
namespace WcfChannelExtensions
{
    /// <summary>
    /// Provides extension methods for WCF Service Channels
    /// </summary>
    public static class WcfChannelExtensions
    {
        /// <summary>
        /// Make WCF service calls without needing to worry about disposing of underlying connection when Faults occur.
        /// </summary>
        /// <typeparam name="T">The channel</typeparam>
        /// <param name="client">The service.</param>
        /// <param name="work">Code to execute.</param>
        /// <example>new ChannelFactory<IFooContract>().CreateChannel().SafeInvoke(service =>
        /// {
        ///     service.Foo();
        /// });</example>
        public static void SafeInvoke<T>(this T client, Action<T> work) where T : ICommunicationObject
        {
            try
            {
                client.Open();
                work(client);
                client.Close();
            }
            catch
            {
                client.Abort();
                throw;
            }
        }
    }
}

Initial URL
http://www.sage.co.uk/devblog

Initial Description
Shows how to generically handle fault exceptions in WCF Services which prevent you relying on using() {} to dispose of the connection.

Initial Title
Extension Method to Correctly Close WCF Connections When Faults Occur

Initial Tags


Initial Language
C#