Any service oriented architecture, including WCF, follows a convention where a client implements a proxy pattern. Proxy allows the client to abstract from the concrete implementations and locations of the service. Client often creates stubs that work well in the compile time, whereas in runtime stubs are resolved and actual calls are made to the service.
Closing WCF client
A client has an inherited responsibility of gracefully closing the connection. It is always recommended to close the proxy client. If the binding between a client and a service is transport-layer sessionful, then closing a proxy is essential to tear down the connection between both parties. Service has a payload threshold defined for concurrent connections. If the number of concurrent connections goes above this threshold linearly then the overall service performance exponentially decreases. This is why it is crucial to dispose of the connection as soon as possible. Closing the proxy also notifies the service instance that it is no longer in use and may be collected by GC. If the client does not close a connection, it is still automatically torn down by WCF timeouts (found in the configuration files).
Aborting WCF client
In the situation where there is a fault in the service-client interaction, the objects on both ends are potentially totally broken. Thus using a proxy after the exception is not advised. Given the WCF binding use transport sessions, the client after a fault would not even be able to close it (if there was no transport layer session then the client could use or close the proxy, but this is not recommended as the configuration of sessions could change). So after a fault has happened the only safe operation is to abort a proxy.
Coding
Close (and Abort on fault) needs to be called for all the client calls, therefore these methods can be moved to a proxy itself
class Client1 : ClientBase<IContract1>, IContract1
{
public void DoWork()
{
try
{
Channel.DoWork();
Close();
}
catch
{
Abort();
throw;
}
}
}
If you enjoyed this post, make sure you subscribe to my RSS feed!
cc69a51c-4367-4251-aae4-04ca22863759|0|.0