Wednesday, September 30, 2015

A good way to manage WCF connections

A typical problem with WCF connections is that programmer can not relay on using statement to manage a connection. A following code is bad:
using (var client = new SomeWCFServiceClient()) 
{
    //Do something with the client 
}
There are few stackoverflow threads about how to do it. But not a single one of them satisfy my typical needs.

This is how I do it.
In a service class:

IBuildServiceDisposable GetBuildWebService()
{
   return _buildWebService.GetBuildWsClient();
}

private WcfDisposableWrapper<IBuildServiceDisposable> WcfService
{
   get
   {
      return new WcfDisposableWrapper<IBuildServiceDisposable>();
   }
}

public Brochure GetBrochures(BrochureRequest param)
{
  return WcfService.Use(
                GetBuildWebService(),
                client => client.GetBrochures(param));
}
Where the service interface inherits from an interface that was auto generated by client proxy (IBuildService) is defined as:
public interface IBuildServiceDisposable : IBuildService, IDisposable, ICommunicationObject {}
The most important part is implementation of WcfDisposableWrapper class itself:
public class WcfDisposableWrapper<T> where T : ICommunicationObject, IDisposable
{
  public TReturnType Use<TReturnType>(T clientProxy, Func<T, TReturnType> codeBlock)
  {
    bool success = false;
    try
    {
       var returnValue = codeBlock(clientProxy);
       clientProxy.Close();
       success = true;
       return returnValue;
    }
    finally
    {
      if (!success)
      {
         clientProxy.Abort();
         clientProxy.Dispose();
      }
    }
  }
}