简体   繁体   中英

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 (eg 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

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.

Creating a ChannelFactory & Opening it is an expensive operation and you should avoid doing it for every call if you care performance.

Your first usecase is not right even with basicHttpBinding because it will potentially create a new channelfactory for each instantation. .NET 3.5 SP1 has introduced some ChannelFactory caching so you might be ok in certain scenarios.

In your 2nd usecase, if you cache and reuse the channelfactory, disposing isn't really nesseaary but keep in mind you/your deployment guy can change the binding @ deployment time and lack of closing/disposing can have a huge impact.

In summary it's always safe to close/dispose and that's why MSDN suggest that.

It really depends on the type of client. For example, if you writing an ASP.NET application that calls into the service, then it is a good idea to cache the proxy because its creation is expensive.

This being said, once you are done with any IDisposable resource, you should dispose it so that the object being disposed gets a chance to release resources it's holding on to so that it can be removed from memory. If an IDisposable object has a Close method, it should be called first.

An excellent article on this fascinating subject can be found here: http://msdn.microsoft.com/en-us/magazine/bb985010.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM