WCF Proxy Close Issue

WCF Proxy need to be closed explicitly and if you are using the using() statement, you have the possibility of loosing the original exception.
You can find details about this issue in the following posts.
http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/b95b91c7-d498-446c-b38f-ef132989c154
https://geekswithblogs.net/marcel/archive/2007/05/01/112159.aspx

Marc Gravell presented a great idea to have a wrapper so as to overcome this issue.
http://marcgravell.blogspot.com/2008/11/dontdontuse-using.html

But that will work only with the proxies got using Service reference.
What if you are getting your service proxy using : factory.CreateChannel().
This blog is about the solution for that.

Here is the full code for the proxy.

public interface IDisposableWrapper : IDisposable
    {
        T BaseObject { get; }
    }
    public class DisposableWrapper : IDisposableWrapper where T : class
    {
        public T BaseObject { get; private set; }
        public DisposableWrapper(T baseObject) { BaseObject = baseObject; }
        protected virtual void OnDispose()
        {
            (BaseObject as IDisposable).Dispose();
        }
        public void Dispose()
        {
            if (BaseObject != null)
            {
                try
                {
                    OnDispose();
                }
                catch (Exception ex){
                    //Log if required
                }
            }
            BaseObject = default(T);
        }
    }

    public class ClientWrapper : DisposableWrapper
        where TProxy : class
    {
        public ClientWrapper(TProxy proxy) : base(proxy) { }
        protected override void OnDispose()
        {
            if ((this.BaseObject as IClientChannel).State == CommunicationState.Faulted)
            {
                (this.BaseObject as IClientChannel).Abort();
            }
            else
            {
               
                (this.BaseObject as IClientChannel).Close();
            }
           
        }
    }

    public static class WCFExtensions
    {
        public static IDisposableWrapper Wrap(
            this TProxy proxy)
            where TProxy : class
        {

            return new ClientWrapper(proxy);
        }
    }

Client Code:
ChannelFactory factory = new ChannelFactory(new WSHttpBinding(), new EndpointAddress("http://localhost:8731/TestWcfServiceLibrary/Service1/"));
            IService1 proxy = factory.CreateChannel();

using (var proxy = proxy.Wrap())
                {
                    //Call the methods  the following wat
                    string s1 = proxy.BaseObject.GetData(10);
                }

Happy coding.!

This article is part of the GWB Archives. Original Author: Sudheer Kumar

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.