gpt4 book ai didi

c# - net.tcp 绑定(bind)上的线程不足 - TCP 错误代码 10061

转载 作者:太空狗 更新时间:2023-10-29 21:56:58 25 4
gpt4 key购买 nike

我在我的 WCF 服务中遇到了一个非常奇怪的错误,当我使用 NetTcpBinding 时,它似乎以某种方式在套接字级别创建了死锁或线程饥饿。我有一个非常简单的自托管服务:

class Program
{
static void Main(string[] args)
{
using (ServiceHost serviceHost = new ServiceHost(typeof(TestService)))
{
serviceHost.Open();
Console.WriteLine("Press <ENTER> to terminate service.");
Console.ReadLine();
serviceHost.Close();
}
Uri baseAddress = new Uri("net.tcp://localhost:8014/TestService.svc");
}
}

[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetData(string data);
}

public class TestService: ITestService
{
public string GetData(string data)
{
Console.WriteLine(data);
Thread.Sleep(5000);
return "Ok";
}
}

配置部分:

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBinding" closeTimeout="00:02:00" openTimeout="00:02:00"
receiveTimeout="00:02:00" sendTimeout="00:02:00" maxBufferSize="2000000000"
maxReceivedMessageSize="2000000000" />
</basicHttpBinding>
<netTcpBinding>
<binding name="netTcpBinding" closeTimeout="00:02:00" openTimeout="00:02:00"
receiveTimeout="00:02:00" sendTimeout="00:02:00" listenBacklog="2000"
maxBufferSize="2000000000" maxConnections="1000" maxReceivedMessageSize="2000000000">
<security mode="None">
<transport protectionLevel="EncryptAndSign" />
</security>
</binding>
<binding name="TestServiceTcpEndPoint">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>

<behaviors>
<serviceBehaviors>
<behavior name="CommonServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceThrottling maxConcurrentCalls="1000" maxConcurrentSessions="1000" maxConcurrentInstances="1000" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ServiceLauncher.TestService" behaviorConfiguration="CommonServiceBehavior">
<endpoint address="" binding="netTcpBinding" bindingConfiguration="netTcpBinding" name="TestServiceTcpEndPoint" contract="ServiceLauncher.ITestService" />
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding" name="TestServiceTcpEndPoint" contract="ServiceLauncher.ITestService" />
<endpoint address="mex" binding="mexHttpBinding" bindingName="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8014/TestService.svc"/>
<add baseAddress="http://localhost:1234/TestService.svc"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>

我有一个客户端,它在许多线程中使用此服务,并为每个线程创建新实例(这是一项要求):

    static void Main(string[] args)
{
for (int i = 0; i < 1000; i++)
{
Thread tr = new Thread(() =>
{
using (var service = new Test.TestServiceClient())
{
var result = service.GetData(i.ToString());
Console.WriteLine(string.Format("{0}: {1} {2}",
DateTime.Now,
result,
Thread.CurrentThread.ManagedThreadId));
}
});
tr.Start();
}
Console.ReadLine();
}

在这种情况下,在一些请求之后,客户端会引发EndpointNotFoundException,TCP 错误代码10061,无法建立连接,因为目标机器主动拒绝了它。请求的数量始终不同,并且它不是服务器部分,因为它仍然在正常状态下工作。我看到它不断收到请求,在这种情况下最奇怪的是。同样奇怪的是,它可以使您的客户端主机在异常发生后“不朽”——这样您就无法通过任何方式杀死它,除非重新启动系统。我很确定问题出在客户端的低套接字级别,并且它以某种方式与如此大量的线程相关联,但我没有成功找到可以解释问题的东西。

最佳答案

每次我看到错误“无法建立连接,因为目标机器主动拒绝它。”问题不在于服务。它通常是连接服务的问题。

一些建议:

  1. Avoid using with WCF Proxies 。你可以从 several reasonable work arounds 中挑选。

  2. 阅读我对 WCF performance, latency and scalability 的回答。除了以老式方式启动线程外,它基本上是相同的测试应用程序。该帖子描述了导致“无法建立连接,因为目标机器主动拒绝它”的所有客户端原因(我能找到),并提供了可以调整的不同 WCF、TCP 和线程池设置。

    <

关于c# - net.tcp 绑定(bind)上的线程不足 - TCP 错误代码 10061,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34681487/

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