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
- Anonymous
June 02, 2008
The comment has been removed - Anonymous
June 26, 2008
ClientBase<T> implements IDisposable. You can use the using statement. using(DataContractCalculatorClient client = new DataContractCalculatorClient()) { //do work }