WCF Client and the “using” Statement
As I was researching patterns and practices for a WCF client implementation, I came across a bug in the .Net implementation of ClientBase
The core of the issue is that ClientBase
There are a few different solutions to this issue. I went with a simple Disposable wrapper…
// Get a disposable MyClientWrapper object
using(var clientWrapper = ClientFactory.GetMyClientWrapper())
{
clientWrapper.Client.DoSomething();
}
public class MyClientWrapper : IDisposable
{
private readonly MyClient _client;
public MyClientWrapper(Binding binding, Uri endpoint)
{
// creates an instance of MyClient, which is derived from ClientBase<T>
_client = new MyClient(binding, new EndpointAddress(endpoint.ToString()));
}
public MyClient Client
{
get { return _client; }
}
public void Dispose()
{
// safe dispose with extension method (below)
_client.CloseProxy();
}
}
// extension method for safe Close() on ClientBase<T>
public static void CloseProxy(this ClientBase<T> proxy) where T : class
{
try
{
if (proxy.State != CommunicationState.Closed
&& proxy.State != CommunicationState.Faulted)
{
proxy.Close(); // may throw exception while closing
}
else
{
proxy.Abort();
}
}
catch (CommunicationException)
{
proxy.Abort();
throw;
}
}