net.tcp绑定上的线程饥饿 - TCP错误代码10061

时间:2016-01-08 16:17:29

标签: c# multithreading wcf sockets net.tcp

我在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之后,由于目标计算机主动拒绝它,因此无法建立连接。请求的数量始终不同,并且它不是服务器部分,因为它仍然在正常状态下工作。而且我看到它不断收到请求,在这种情况下最奇怪的是什么。奇怪的是它可以使你的客户端主机在异常后成为“不朽” - 所以除了重新启动系统之外,你不能以任何方式杀死它。我很确定问题是在客户端的低套接字级别,并且它以某种方式与如此大量的线程连接,但我没有成功找到可以解释问题的东西。

2 个答案:

答案 0 :(得分:1)

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

一些建议:

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

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

答案 1 :(得分:0)

您可能会对Windows中的并发TCP / IP连接达到内部限制。看看这篇文章,看看它是否有帮助:

http://smallvoid.com/article/winnt-tcpip-max-limit.html