Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it important to dispose/close a WCF client proxy

I heard that it's essential to Dispose (or Close) a WCF client proxy even when

  • you're not using sessions
  • there are no unmanaged resources that need deterministic clean up (e.g. open sockets)

For example, when using a BasicHttpBinding with the default binding configuration, this should be fine even in a popular web page, right?

var clt = new MyServiceClient();
clt.PlaceOrder(foo);
// no dispose

or

var clt = new ChannelFactory<IOrderService>().CreateChannel();
clt.PlaceOrder(foo);

Thanks

like image 808
ConfusedNoob Avatar asked Aug 25 '11 02:08

ConfusedNoob


1 Answers

Its good practice to close things (and dispose of them) when you're done with them. (Would you leave a file stream open even though you're through reading/writing to/from it?) Off-hand, I can see a few reasons:

  1. The server (can/will) have a limited number of active connections it maintains. The sooner you dispose of your service, the sooner the next client has availability to use that slot. (Why wait for a timeout if you are in-fact through?)
  2. Avoid the excess overhead of an inactive connection. Granted resources are "plentiful" these days, but the less overhead you keep the better your performance in the end will be.
  3. You reduce the risk of errors/exceptions due to timeout by disposing the client when it's through.
  4. By closing it when you're through you're effectively keeping the server logs clean. In the end, even if the client doesn't show it, the server can end up with timeout exceptions showing up in the log due to a dormant connections that weren't taken care of when they should have been.
  5. MSDN says to (Note the 4th bullet in the WCF client objects list).

Just a few of the reasons I can think of off the top of my head.

like image 186
Brad Christie Avatar answered Oct 05 '22 22:10

Brad Christie