What Was I Thinking?

Follies & Foils of .NET Development
posts - 95 , comments - 352 , trackbacks - 0

Clean Up WCF Clients : The Right Way

As I've done more and more WCF work recently, I've noticed an intermittent problem running my unit tests.

The host seemingly hangs for no obvious reason.  Eventually the connection times out and produces the following in the service log:

System.ServiceModel.CommunicationObjectAbortedException, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Stopping the host and client and re-running the test allows it to pass without issue. 

The problem is related to the way I managed my WCF channels and client proxies.  In my code I make use of the ChannelFactory<T> object to create my wcf channels dynamically from my configuration information, like the following:

 

       #region IdentityService Proxy
        private static IIdentityService _identitySvc;
        private static IIdentityService identitySvc
        {
            get
            {
                if (_identitySvc == null)
                {
                    var factory = new ChannelFactory<IIdentityService>("IdentityService");
                    _identitySvc = factory.CreateChannel();
                }
                return _identitySvc;
            }

        }
        #endregion
 

The client proxy produced implements the typed interface "IIdentityService" in the above example. It natively supports no operations for channel management and cleanup. However, failing to clean up the client proxy may cause channel timeouts and resource blocking on the server. So channel cleanup is important, but if its not implemented in the client proxy, how do you manage it?

The secret lies in the casting.  The transparent proxy produced implements a number of useful interfaces.  For our purposes we care about IDisposable and IClientChannel. When we're done with the proxy,  we must close the channel and dispose of it.  I've seen some examples like this:

using (IClientChannel client = (IClientChannel)channelFactory.CreateChannel())
{
    IIdentityService proxy = (IIdentityService)client;
}

This is Bad. Yes, you are disposing of the channel resource, but you haven't closed the wcf channel, you've only disposed of your handle to it. You must explicitly call close then dispose on the proxy like so:

            if (_identitySvc != null)
             {
                 ((IClientChannel)_identitySvc).Close();
                 ((IDisposable)_identitySvc).Dispose();
                 _identitySvc = null;
             }
 

Optionally, since IClientChannel implements IDisposable, you could call ((IClientChannel) proxy).Dispose().  If you prefer, and your design allows, you can still use the using statement, just be sure to add try/catch blocks and call the close() method on the casted proxy before the closing brace. I tend to have static references to my proxy classes so I have to explicitly call close() and dispose() when I complete my WCF operation.

Now that I'm properly cleaning up my proxies, my WCF services run all my tests without hanging.

Print | posted on Friday, May 23, 2008 5:12 PM | Filed Under [ Visual Studio ]

Feedback

Gravatar

# re: Clean Up WCF Clients : The Right Way

Does this occur if you use a http channel as well?
5/24/2008 12:04 PM | flalar
Gravatar

# re: Clean Up WCF Clients : The Right Way

Yes, I was using wsHttpBinding when I experienced the timeouts and errors in the service log.

I've seen the same behavior over http and TCP. It makes sense if the wcf channel requires explicit closing.
5/24/2008 12:36 PM | wtfChris
Gravatar

# re: Clean Up WCF Clients : The Right Way

Thank you very much!
This helped me alot! I was closing the channel but not disposing it properly, no everything works great
5/30/2008 4:41 AM | bizarre
Gravatar

# re: Clean Up WCF Clients : The Right Way

Note that if you call .Close() on a faulted client it will throw. Switch on the CommunicationState and if it is Faulted then to .Abort() rather than .Close()
6/4/2008 10:22 AM | David Bates
Gravatar

# re: Clean Up WCF Clients : The Right Way

Yep this was the same issue we were having with our app, and we started clearing channels to resolve the issue.

BTW we were also using wshttp binding.
9/25/2008 8:09 PM | Sunny Nagi
Gravatar

# re: Clean Up WCF Clients : The Right Way

what David Bates said is 100% true, but you cant do this in the dispose method as the state property may return Opened status even if its in faulted state, and your code will execute the Close() method and throw an exception.

so what you need to do is to register faulted event and abort the channel there as following:

channel.Faulted += new EventHandler(Current_Faulted);

static void Current_Faulted(object sender, EventArgs e)
{
(sender as ICommunicationObject).Abort();
}
1/25/2009 9:39 PM | Yazeed Hamdan
Gravatar

# re: Clean Up WCF Clients : The Right Way

How can this work with PollingDuplexHttpBinding?
7/16/2009 10:58 AM | Sven
Gravatar

# re: Clean Up WCF Clients : The Right Way

Thanks this really helped me out.
12/6/2009 8:57 PM | James
Gravatar

# re: Clean Up WCF Clients : The Right Way

Is this client-side or server-side code?
10/3/2011 12:11 PM | Chu
Gravatar

# re: Clean Up WCF Clients : The Right Way

What's the point of implementing IDisposable if you don't close resources?
2/9/2012 10:53 AM | Luzian
Gravatar

# re: Clean Up WCF Clients : The Right Way

Can we enable WCF service to close the client connections, if it does not close/dispose it.
2/3/2013 5:22 AM | Om P
Gravatar

# re: Clean Up WCF Clients : The Right Way

Thanks!! It helped me a lot! You are amazing! Thanks a lot!
3/7/2014 7:06 AM | Alison
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: