What Was I Thinking?

Follies & Foils of .NET Development

  Home  |   Contact  |   Syndication    |   Login
  28 Posts | 0 Stories | 58 Comments | 0 Trackbacks

News

Archives

Post Categories

Check These Out

Gurus

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.

posted on Friday, May 23, 2008 5:12 PM

Feedback

# re: Clean Up WCF Clients : The Right Way 5/24/2008 12:04 PM flalar
Does this occur if you use a http channel as well?

# re: Clean Up WCF Clients : The Right Way 5/24/2008 12:36 PM wtfChris
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.

# re: Clean Up WCF Clients : The Right Way 5/30/2008 4:41 AM bizarre
Thank you very much!
This helped me alot! I was closing the channel but not disposing it properly, no everything works great

# re: Clean Up WCF Clients : The Right Way 6/4/2008 10:22 AM David Bates
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()

# re: Clean Up WCF Clients : The Right Way 9/25/2008 8:09 PM Sunny Nagi
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.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: 
Please add 4 and 6 and type the answer here: