简体   繁体   English

wcf客户端初始化和方法调用

[英]wcf client initialization and method call

I am new to WCF and am interested what is the best practice to call a service method. 我是WCF的新手,并且对调用服务方法的最佳实践感兴趣。 The application will consist in a bunch of forms and I would like to know if it's better to declare a global client instance for each form and then just call the methods when needed. 该应用程序将包含许多表格,我想知道为每个表格声明一个全局客户端实例是否更好,然后在需要时调用方法。 Or is it better to instantiate the client proxy before each method call and close it right after. 还是在每个方法调用之前实例化客户端代理并在之后立即关闭它更好。

I believe creating a global var of client for each form will do for you, no need to instantiate service each time before calling the service method. 我相信为每种形式创建一个全局的客户端变量将对您有所帮助,无需每次调用​​service方法之前都实例化服务。

public MyService ser {get; set;} 

Inside class constructor. 内部类构造函数。

ser = new MyService();

The most simple and safest way is constructing client proxy every time you use it. 最简单,最安全的方法是每次使用时都构造客户端代理。 The drawback of this approach is loosing perfomance, but depending on your binding (http, net.tcp, etc) and service mode (PerCall, Statefull, Singleton) you will not notice the difference (see this answer WCF Proxy Pooling - Is it worth it? ). 这种方法的缺点是缺乏性能,但是根据您的绑定(http,net.tcp等)和服务模式(PerCall,Statefull,Singleton),您将不会注意到差异(请参阅此答案WCF代理池-是否值得)它吗? )。

If you create a proxy on the form level, when this proxy is in the faulted state (because of the connection problems), you won't be able to reuse it and will have to reopen form. 如果在表单级别创建代理,则当该代理处于故障状态(由于连接问题)时,将无法重用它,而必须重新打开表单。

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

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