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.

Comments (2)
  1. Zeez says:

    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!

  2. an_phu says:

    ClientBase<T> implements IDisposable.

    You can use the using statement.

    using(DataContractCalculatorClient client = new DataContractCalculatorClient())

    {

    //do work

    }

Comments are closed.

Skip to main content