/ Published in: C#
Shows how to generically handle fault exceptions in WCF Services which prevent you relying on using() {} to dispose of the connection.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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; } } } }
URL: http://www.sage.co.uk/devblog