简体   繁体   English

何时关闭WCF客户端?

[英]When to close WCF client?

I've put an instance of the client proxy for the WCF service into a property on the App class so I can get it from anywhere in the app. 我已将WCF服务的客户端代理实例放入App类的属性中,以便我可以从应用程序的任何位置获取它。

I am not closing the client, I'm leaving it open for the duration of the app. 我没有关闭客户端,我在应用程序期间将其保持打开状态。 The main reason for this is that if I were to follow the // Comment in the WCF service mex page (the one you get if you point a browser at the WCF service url) it says // Always close the client. 这样做的主要原因是,如果我在WCF服务mex页面(如果你将浏览器指向WCF服务网址时得到的那个)中按//注释,则表示//始终关闭客户端。 client.Close(); client.Close();

which is fine, except if I call client.Close() right after I make a call to client.SomeAsync() method then it's being closed before the results come back. 这很好,除非我在调用client.SomeAsync()方法之后调用client.Close()然后在结果返回之前关闭它。 Should I be putting the close into the Completed() method? 我应该接近Completed()方法吗? Or should I just forget about closing it, as once its closed I have to create a new instance of the client proxy (might as well not store it in the App.property if that is the case. 或者我应该忘记关闭它,因为一旦关闭它我必须创建客户端代理的新实例(如果是这种情况,也可以不将它存储在App.property中。

thanks, Stephen 谢谢,斯蒂芬

You should close it as advised. 你应该按照建议关闭它。 And yes, if you're using the async methods then you have to close it only after the call completes. 是的,如果您使用的是异步方法,则必须在调用完成后关闭它。

Creating (opening) and closing clients is the norm for WCF clients. 创建(打开)和关闭客户端是WCF客户端的标准。 There is no noticeable performance penalty for continuously creating and closing new clients. 持续创建和关闭新客户端没有明显的性能损失。

Explicitly close it preferably in a finally{} of your Completed method. 最好在您的Completed方法的finally {}中明确地关闭它。 For whatever reason if the client stays connected you will start blocking other client calls. 无论出于何种原因如果客户端保持连接,您将开始阻止其他客户端呼叫。 As per the HTTP RFC: 根据HTTP RFC:

Defined in 1999 (RFC 2616) “clients that use persistent connections should limit the number of simultaneous connections that they maintain to a given server. 定义于1999年(RFC 2616)“使用持久连接的客户端应限制它们维护到给定服务器的同时连接数。 A single-user client SHOULD NOT maintain more than 2 connections with any server or proxy. 单用户客户端不应该与任何服务器或代理保持2个以上的连接。 A proxy SHOULD use up to 2*N connections to another server or proxy, where N is the number of simultaneously active users. 代理应该使用最多2 * N个连接到另一个服务器或代理,其中N是同时活动用户的数量。 These guidelines are intended to improve HTTP response times and avoid congestion.” Since developers are using AJAX or AJAX-like requests to update a Web page the http limits are discussed more and more. 这些指南旨在改善HTTP响应时间并避免拥塞。“由于开发人员使用AJAX或类似AJAX的请求来更新网页,因此越来越多地讨论http限制。

With a load if you are not closing your connections it is very possible you will start blocking. 如果您没有关闭连接,那么您很可能会开始阻止。 I am of course assuming this is HttpBinding. 我当然假设这是HttpBinding。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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