gpt4 book ai didi

.net - 为什么 WCF 不支持服务端超时?

转载 作者:行者123 更新时间:2023-12-03 12:17:55 26 4
gpt4 key购买 nike

我们最近发现 WCF 不支持服务端的超时操作(注意,服务端,而不是客户端)。虽然客户端在指定时间后断开连接,但我们的测试表明,对于 netNamedPipeBinding、netTcpBinding 和 basicHttpBinding,我们指定的没有超时将导致服务操作在调用后停止。以下是我们尝试的特定绑定(bind)配置:

<bindings>
<netNamedPipeBinding>
<binding name="TestServiceBindingConfigurationNamedPipe"
receiveTimeout="00:00:05"
sendTimeout="00:00:05"
closeTimeout="00:00:05"
openTimeout="00:00:05" />
</netNamedPipeBinding>
<netTcpBinding>
<binding name="TestServiceBindingConfigurationTcp"
receiveTimeout="00:00:05"
sendTimeout="00:00:05"
closeTimeout="00:00:05"
openTimeout="00:00:05" />
</netTcpBinding>
<basicHttpBinding>
<binding name="TestServiceBindingConfigurationBasicHttp"
receiveTimeout="00:00:05"
sendTimeout="00:00:05"
closeTimeout="00:00:05"
openTimeout="00:00:05" />
</basicHttpBinding>
</bindings>

我们的测试服务实现如下所示:
public class TestServiceImpl : ITestService
{
public TestResult TestIt(TestArgs args)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

// this is a contrived example, but it shows that WCF never stops this thread
while (true)
{
Console.WriteLine("{0}> I'm running forever...", stopwatch.Elapsed);
}

return new TestResult {Result = "Args were " + args.Args};
}
}

使用 netNamedPipeBinding 和 netTcpBinding,我们的客户端应用程序将在 5 秒后超时,但服务将无限期地继续运行。

这带来了我的问题 – 这是一个错误吗?如果服务运行的时间比预期的长,WCF 是否不想让服务超时?

从我的角度来看,与此相关的一些潜在负面问题包括:
  • 服务实例的默认限制是 10 个。因此,如果您的服务中有坏代码永远运行并且它被命中 10 次,您的服务将完全关闭;不接受新的连接。
  • 没有任何可见性的事实是服务永远运行 - 缺少自定义日志记录或可能使用性能计数器
  • 如果没有其他机制使操作超时,则服务调用正在使用的任何资源都可能被无限期地持有(例如,SQL 行、页和表锁)。
  • 最佳答案

    如果你有永远运行的糟糕代码,超时可能只会让事情变得更糟。 如果可能的话,修复错误的代码! 参见 Eric Lippert 的文章,Careful with that axe ,关于这类情况。

    如果这是在开发中,您可能想尝试设置 System.Threading.Timer调用 serviceCallThread.Abort()在您的服务实现中。但是,请确保您在返回之前已彻底解除计时器 - 这种方法是 极易出错 ,由于并发问题的混合,不拥有服务调用到达的线程,ThreadAbortException 的奇怪行为,以及 Eric 解释的关于盲目终止杂草中的代码的问题。

    关于.net - 为什么 WCF 不支持服务端超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4974640/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com