何时关闭 WCF 客户端? [英] When to close WCF client?

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

问题描述

我已将 WCF 服务的客户端代理实例放入 App 类的一个属性中,以便我可以从应用程序的任何位置获取它.

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 服务 mex 页面中的//注释(如果您将浏览器指向 WCF 服务 url,则会得到该页面),它说//始终关闭客户端.client.Close();

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. client.Close();

这很好,除非我在调用 client.SomeAsync() 方法后立即调用 client.Close() 然后它在结果返回之前被关闭.我应该将关闭放入 Completed() 方法中吗?或者我应该忘记关闭它,因为一旦它关闭,我必须创建一个客户端代理的新实例(如果是这种情况,也可能不将它存储在 App.property 中.

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. Should I be putting the close into the Completed() method? 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.

谢谢,斯蒂芬

推荐答案

您应该按照建议关闭它.是的,如果您使用的是异步方法,那么您必须在调用完成后才关闭它.

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.

创建(打开)和关闭客户端是 WCF 客户端的规范.持续创建和关闭新客户端不会造成明显的性能损失.

Creating (opening) and closing clients is the norm for WCF clients. There is no noticeable performance penalty for continuously creating and closing new clients.

这篇关于何时关闭 WCF 客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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