What is the best practise for Disposing/Cleaning up a web service proxy instance after synchronous usage?
How does the answer differ if the proxy class is derived from SoapHttpClientProtocol
versus ClientBase<T>
?
Background
I'm trying to figure out why one of my WCF web services sometimes seems to get into a state where it no longer reponds to service calls. Basically it seems like it hangs and for now I don't really have any hard data to figure out what's going on when this occurrs.
One thing that I suspect might be an issue is the fact that this WCF service is itself doing web service calls to a few other services. These other services are called (synchronously) using proxies that are derived from SoapHttpClientProtocol
(made using wsdl.exe) and at this time these proxy instances are left to be cleaned up by the finalizer:
...
var testProxy = new TestServiceProxy();
var repsonse = testProxy.CallTest("foo");
// process the reponse
...
So should I simply wrap these up in a using(...) { ... }
block?
...
using(var testProxy = new TestServiceProxy())
{
var repsonse = testProxy.CallTest("foo");
// process the reponse
}
...
What if I were to change these proxy classes to be based on ClientBase<T>
by recreating them using svcutil.exe
? Based on my research so far, it seems the Dipose()
method of classes derived from ClientBase<T>
will internally call the Close()
method of the class and this method might in turn throw exceptions. So wrapping a proxy based on ClientBase<T>
in a Using()
is not always safe.
So to reiterate the question(s):
- How should I clean up my web service proxy after using it when the proxy is based on
SoapHttpClientProtocol
? - How should I clean up my web service proxy after using it when the proxy is based on
ClientBase<T>
?
Dispose
is implemented byComponent
, butAbort
is implemented byWebClientProtocol
. Looking at the code it appears thatDispose
couldn't know about the request. msdn.microsoft.com/en-us/library/ff647786.aspx seems to back that up (under "Abort Connections for ASP.NET Pages That Timeout Before a Web Services Call Completes"). For WCF, see #574372 – Excurrent