WCF:代理打开和关闭 - whaaa? [英] WCF: Proxy open and close - whaaa?

查看:25
本文介绍了WCF:代理打开和关闭 - whaaa?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在维护一个使用 WCF 的 Windows 窗体应用程序,并在内部使用 Net.TCP.我们连接的生命周期是 GET/USE/CLOSE.

I am maintaing a Windows Forms application using WCF and are using Net.TCP internally. The lifecycle of our connections is GET/USE/CLOSE.

我们遇到了应用程序池崩溃而没有任何痕迹的问题.在查看 netstat 时,我可以看到我何时进入应用程序,因为我们有一个登录服务.然而,即使我们在 using 语句中创建代理,netstat 中的连接也不会消失,直到我物理关闭应用程序.

We are having a problem with the application pool crashing with no trace. In looking at netstat, I can see when I come into the application as we have a login service. However, even though we are creating the proxy in a using statement, the connection in netstat does not go away until I physically close the application.

这样对吗?我应该在客户端上做一些不同的事情来强制关闭连接吗?

Is this right? Should I be doing something different on the client to force the connection to close?

因此,如果连接保持打开状态,它是否会在 openTimeout 设置期间保持打开状态,然后断开连接?

So if the connection stays open, does it stay open for the duration of the openTimeout setting and then gets torn down?

推荐答案

Microsoft 表示您始终必须在最后关闭连接(请参阅 示例 在 MSDN).我在这个 有关 WCF 处置处理的文章:

Microsoft says that you always have to close the connection at the end (see the example at MSDN). I've found the following pattern in this article about WCF disposal handling:

WCFServiceClient c = new WCFServiceClient();

try
{
    c.HelloWorld();
}
catch
{
    // acknowledge the Faulted state and transition to Closed
    c.Abort();

    // handle or throw
    throw;
}
finally
{
    c.Close();
}

文章说你应该避免using,因为它没有正确关闭和处理WCF服务客户端对象,你应该用try ... catch ... finally 块而不是如上所示 - 这样您就可以处理异常(将中止然后重新抛出或处理异常),并且您还负责最终关闭连接.这也在 Microsoft 的 WCF 故障排除提示中明确说明.

The article says you should avoid using since it does not properly close and dispose the WCF service client object, you should do it with a try ... catch ... finally block instead as shown above - this way you're dealing with exceptions (which will abort and then re-throw or handle the exception) and also you take care of finally closing the connection. This is also clearly stated in Microsoft's WCF troubleshooting hints.

注意: finally 中的 c.Close() 在异常(故障状态)的情况下不会造成任何伤害,因为我们在重新抛出异常之前调用了 c.Abort() 所以 c.Close() 在这种情况下实际上什么都不做.但是,如果没有异常发生,那么c.Close()实际上是正常执行的,连接按预期关闭.

Note: The c.Close() in the finally does not do any harm in case of an exception (faulted state), because we call c.Abort() before the exception is re-thrown so the c.Close() does actually nothing in this case. However, if no exception occurs, then c.Close() is actually executed normally and the connection closes as expected.

如果您的 WCF 服务 以一种奇怪的方式运行,有很多(其他)事情可能会导致这种情况 - 此处 您可以找到一些调试提示.

If your WCF service behaves in a strange way, there are many (other) things which could cause this - here you can find some debugging hints.

这篇关于WCF:代理打开和关闭 - whaaa?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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