如何让WCF自动关闭连接?
当我使用内置 Visual Studio 模板创建 WCF 应用程序并尝试在循环中调用它时,只有 5 个请求通过。 然后该服务停止响应。 我可以解决这个问题的唯一方法是在每次调用后关闭连接。
我知道您应该自行清理,但我也知道您不必使用 Web 服务来执行此操作。 许多将使用我们服务的人不会关闭他们的连接。
有没有办法通过 WCF 获得相同的行为?
这是我的配置
<system.serviceModel>
<services>
<service name="WorkflowLibrary1.Workflow1" behaviorConfiguration="WorkflowLibrary1.Workflow1.Service1Behavior">
<endpoint address="" binding="wsHttpContextBinding" contract="WcfServiceLibrary1.IService1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WorkflowLibrary1.Workflow1.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
When I create a WCF application using the built-in Visual Studio templates and try calling it while in a loop, only 5 requests get through. The service then stops responding. The only way that I can get around this is to close the connections after each call.
I know that you are supposed to clean up after yourself, but I also know that you didn't have to do this with web services. A lot of the people who are going to be hitting our service won't close their connection.
Is there a way to get the same behavior with WCF?
Here is my config
<system.serviceModel>
<services>
<service name="WorkflowLibrary1.Workflow1" behaviorConfiguration="WorkflowLibrary1.Workflow1.Service1Behavior">
<endpoint address="" binding="wsHttpContextBinding" contract="WcfServiceLibrary1.IService1"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WorkflowLibrary1.Workflow1.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先阅读此内容 -> 避免使用语句出现问题: http://msdn.microsoft.com/en -us/library/aa355056.aspx
如果您想要比上面链接提供的更强大的解决方案,我建议这样做:
http://bloggingabout.net/ blogs/erwyn/archive/2006/12/09/WCF-Service-Proxy-Helper.aspx (代码)
http://bloggingabout。 net/blogs/erwyn/archive/2007/01/30/usage-of-the-wcf-serviceproxyhelper.aspx (usage/samples)
瞧!
Read this first -> Avoiding Problems with the Using Statement: http://msdn.microsoft.com/en-us/library/aa355056.aspx
If you want a more robust solution than provided in by the link above, I recommend this:
http://bloggingabout.net/blogs/erwyn/archive/2006/12/09/WCF-Service-Proxy-Helper.aspx (code)
http://bloggingabout.net/blogs/erwyn/archive/2007/01/30/usage-of-the-wcf-serviceproxyhelper.aspx (usage/samples)
Voila!
5 个连接可能来自服务器 - 您可以通过服务器
serviceThrotdling
行为来定义最大打开会话数、最大并发调用数和最大服务器实例数。同时,这是否会让您增加同时打开的会话数量,我仍然建议您自己进行适当的清理 - 即使您在过去不需要这样做......
我建议将客户端代理的使用包装到
using
语句中,如下所示:更新: 正如评论者正确指出的那样,这存在一定的问题,因为客户端可能会抛出处置时例外。 因此,这可能并不能很好地工作 - 或者您需要在其周围包装一个
try...catch
来处理关闭客户端代理导致问题的情况。请参阅避免使用语句出现问题
这样,客户端代理就是当 using 块的范围结束,并且从客户端到服务器的通道被释放并且服务器准备好接收来自另一个客户端的另一个调用时,自动关闭并处置。
另外,对于 wsHttpContextBinding,您应该检查是否确实需要默认情况下打开的会话 - 推荐的最佳实践是在服务器上使用每次调用实例,例如每个调用者实例化一个新的服务器对象。 会话引入了一系列新问题和潜在的陷阱,因此我会尝试仅在确实需要时使用它们(并从中受益) - 否则关闭会话。
马克
The 5 connections probably comes from the server - you can define the number of maximum open sessions, max concurrent calls, and max server instances by means of the servers
serviceThrottling
behavior.At the same time, will this will allow you to increase the number of concurrently open sessions, I would still recommend to properly clean up after yourself - even if you didn't have to in the olden days.....
I would suggest wrapping the use of your client proxy into a
using
statement like so:Update: as a commentor has rightfully pointed out, this has it's share of problems, since the client might throw an exception upon being disposed. So this might not really work all that well - or you need to wrap a
try...catch
around it to handle those cases where the closing of the client proxy causes an issue.See Avoiding Problems with the Using Statement
That way, the client proxy is automatically closed and disposed of when the scope of the using block ends, and your channel from the client to the server is released and the server is ready to receive another call from another client.
Also with the wsHttpContextBinding you should check whether you really need the sessions that are on by default - the recommended best practice would be to use per-call instancing on the server, e.g. each caller instantiates a new server object. Sessions introduce a whole slew of new problems and potential pitfalls, so I would try to only use them when I really really have to (and get a benefit from it) - otherwise turn off sessions.
Marc
另请查看 WCF 动态客户端代理。 它会在代理完成后自动清理。
Also have a look at the WCF Dynamic Client Proxy. It'll automatically clean-up after your proxy.