Geeks With Blogs
Sudheer Kumar ASP.Net, C#, BizTalk, MSBuild, WPF, WCF, WF....
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
http://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<T> : IDisposable
    {
        T BaseObject { get; }
    }
    public class DisposableWrapper<T> : IDisposableWrapper<T> 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<TProxy> : DisposableWrapper<TProxy>
        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<TProxy> Wrap<TProxy>(
            this TProxy proxy)
            where TProxy : class
        {

            return new ClientWrapper<TProxy>(proxy);
        }
    }


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

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

Happy coding.! Posted on Tuesday, September 1, 2009 9:40 AM | Back to top


Comments on this post: WCF Proxy Close Issue

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Sudheer Kumar | Powered by: GeeksWithBlogs.net | Join free