Welcome to MSDN Blogs Sign in | Join | Help

Technical Weblog of Eric Charran

A Journal of My Technical Achievements and Challenges
HOWTO: Properly Utilize a WCF Service Proxy Class in Code

One critical thing when using proxy classes is to be aware of how to properly call methods and then close the proxy.  Because the proxy makes HTTP requests, (assuming your service is housed within IIS), unless you call .Close() on the proxy, those requests will be held open in IIS and threads will remain dedicated to them.  Thus, without closing your proxy, you will soon have sopped up IIS resources with repeated calls to the service from your application.  Eventually, you will see an error with text similar to the following:

"TimeoutException was Unhandled"

The request channel timed out while waiting for a reply after <Insert Time Interval Here>

The best practice is to call .Close() on your service proxy after each call.  Call it in a finally block and ensure to check to see if the state on the proxy is .Opened first.

Posted: Thursday, May 22, 2008 1:32 PM by echarran

Comments

Zeez said:

Thanks, very simple and direct to the point article. But what if a client got abruptly aborted, thus, the .Close() did not get called? Is there a way the server can free those unused connections with the 'just-aborted' client?

Thank You!

# June 2, 2008 2:04 PM

an_phu said:

ClientBase<T> implements IDisposable.

You can use the using statement.

using(DataContractCalculatorClient client = new DataContractCalculatorClient())

{

//do work

}

# June 26, 2008 11:18 PM
Anonymous comments are disabled
Page view tracker