C# 套接字连接已中止

C# 套接字连接已中止,c#,wcf,C#,Wcf,我有一个WCF服务,作为windows服务托管,由客户端通过代理类调用。代理类中的方法如下所示: public CustomerResponse GetCustomers(CustomerRequest request) { try { return Channel.GetCustomers(request); } catch (Exception ex) {

我有一个WCF服务,作为windows服务托管,由客户端通过代理类调用。代理类中的方法如下所示:

    public CustomerResponse GetCustomers(CustomerRequest request)
    {
        try
        {
            return Channel.GetCustomers(request);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
<services>
  <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
    <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
      contract="ICustomerService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>       
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8542/CustomerService" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehaviour">

      <serviceMetadata httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
GetCustomer目前从我的数据库中返回大约1000条记录。问题是:

<services>
  <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
    <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
      contract="ICustomerService">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>       
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8542/CustomerService" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="DefaultBehaviour">

      <serviceMetadata httpGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
  • 我已经在4台不同的电脑上运行了我的程序,我只在其中2台电脑上发现了这个错误。其他两个都很好,都具有相同的设置

  • <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  • 在有此问题的2台电脑上,当我将数据库中的客户记录数从1000减少到200时,问题就解决了

  • <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  • 我已经在配置文件中最大化了我知道的所有设置

  • <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    这是异常消息

    <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    套接字连接已中止。这可能是由于处理消息时出错、远程主机超过接收超时或基础网络资源问题造成的。本地套接字超时为“00:00:59.9863281”

    <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    我非常确定这不是超时问题,因为异常在不到一秒钟内返回

    <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    这是我在服务器上的配置文件

    <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    

    <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    
    

    <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    这是MVC项目的配置文件

    <services>
      <service name="CustomerService" behaviorConfiguration="DefaultBehaviour">
        <endpoint address="CommonService" binding="netTcpBinding" bindingConfiguration="customTcpBinding"
          contract="ICustomerService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>       
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8542/CustomerService" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DefaultBehaviour">
    
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    <bindings>
          <netTcpBinding>
            <binding name="customTcpBinding" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
              <security mode="None"/>
              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
            </binding>
          </netTcpBinding>
        </bindings>
    
        <endpoint address="net.tcp://localhost:8542/CustomerService" binding="netTcpBinding" bindingConfiguration="customTcpBinding" contract="ICustomerService" name="NetTcpBinding_CustomerService">
            <identity>
              <dns value="localhost"/>
            </identity>
        </endpoint>
    
    
    
    有问题的频道的生命周期是什么?我注意到它是一个TCP绑定,理论上可以保持打开状态。但是,如果TCP连接在请求之间保持打开状态的时间过长,则.net保留关闭TCP连接的权利。我注意到你没有增加通话间隔的超时时间。不,我不需要任何超过1分钟的通话时间。我也在我的帖子中提到,我在不到一秒钟的时间内得到了错误。在我测试过的4台电脑中,有2台工作正常。那么在通话之间呢?看起来你的代理保留的时间太长了。除非你同时发出1000个不同的请求……小提示:永远不要做
    尝试{}catch(Exception ex){throw ex;}
    。所做的只是把堆栈跟踪搞砸了。如果必须在catch块中执行某些操作,则再次抛出相同的异常,然后使用
    catch(exception ex){/*dosomething,然后*/throw;}