为什么很重要处置/关闭WCF客户端代理 [英] Why is it important to dispose/close a WCF client proxy

查看:135
本文介绍了为什么很重要处置/关闭WCF客户端代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说,这是必须的Dispose(或关闭)WCF客户端代理,即使

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

  • 在您不使用会话
  • 在有需要确定的清理没有非托管资源(例如,开放式插座)

例如,使用的默认绑定配置basicHttpBinding的时候,这应该是罚款,甚至在一个受欢迎的网页,对吧?

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

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

感谢

推荐答案

创建的ChannelFactory&安培;打开它是一个昂贵的操作,你应该避免做它的每一个电话,如果你不在乎性能。

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

您第一个用例是不正确的,甚至与basicHttpBinding的,因为这将有可能为每个instantation一个新的ChannelFactory。 .NET 3.5 SP1已经推出了一些的ChannelFactory缓存,所以你可能会确定在某些情况下。

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.

在你的第二个用例,如果缓存和重用的ChannelFactory,处理是不是真的nesseaary但请记住,你/你的部署家伙能更改绑定@部署时间并没有闭合/配置可以产生巨大的影响。

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.

在总结它总是安全的关闭/处置,这就是为什么MSDN建议。

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

这篇关于为什么很重要处置/关闭WCF客户端代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆