I heard that it's essential to Dispose (or Close) a WCF client proxy even when
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:
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.