WCF传输安全性:套接字连接已中止

时间:2015-06-03 08:41:34

标签: wcf wcf-security

我在运输安全性方面遇到了问题。

我有2个服务(A& B)在同一台服务器上运行。服务A将呼叫服务B执行某项任务。没有任何安全保障,我可以很好地沟通。但是当我使用以下设置打开传输安全性时:

  • 安全模式=传输
  • TransportClientCredentialType = Windows
  • ProtectionLevel = EncryptAndSign

当服务A呼叫服务B时出现错误:

  

System.ServiceModel.CommunicationException:套接字连接已中止。这可能是由于处理消息的错误或远程主机超出接收超时或基础网络资源问题引起的。本地套接字超时为'00:00:09.7810000'。 ---> System.IO.IOException:读取操作失败,请参阅内部异常。 ---> System.ServiceModel.CommunicationException:套接字连接已中止。这可能是由于处理消息的错误或远程主机超出接收超时或基础网络资源问题引起的。本地套接字超时为'00:00:09.7810000'。 ---> System.Net.Sockets.SocketException:远程主机强制关闭现有连接

我尝试将接收和发送超时更改为5分钟,但我仍然得到大致相同的超时持续时间相同的错误。唯一的区别是我需要等待5分钟而不是1分钟。

任何人都可以提供有关原因和解决方法的见解吗?

附件是两个服务的配置文件:

ServiceA

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <compilation targetFramework="4.5" debug="true" defaultLanguage="c#" />
  </system.web>
  <system.serviceModel>
    <protocolMapping>
      <remove scheme="net.tcp" />
      <add scheme="net.tcp" binding="netTcpBinding" bindingConfiguration="ReliableTCP" />
    </protocolMapping>
    <client/>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexTag">
          <serviceMetadata httpGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="tryBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <customBinding>
        <binding name="mexTcp">
          <tcpTransport portSharingEnabled="true" />
        </binding>
      </customBinding>
      <netTcpBinding>
        <binding name="ReliableTCP" portSharingEnabled="true" sendTimeout="00:05:00" receiveTimeout="00:05:00" 
                 maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
          <reliableSession enabled="true" />
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="mexTag" name="Test.Service.ServiceAImpl">
        <endpoint address="net.tcp://app-svr:10010/ServiceA/ServiceAImpl/" behaviorConfiguration="tryBehavior"
          binding="netTcpBinding" bindingConfiguration="ReliableTCP" contract="Test.Service.IServiceA" />
        <endpoint address="net.tcp://app-svr:10012/ServiceA/ServiceAImpl/mex"
          binding="customBinding" bindingConfiguration="mexTcp" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

ServiceB

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <compilation targetFramework="4.5" debug="true" defaultLanguage="c#" />
  </system.web>
  <system.serviceModel>
    <client>
      <endpoint address="net.tcp://app-svr:10010/ServiceA/ServiceAImpl/"
        binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IServiceA"
        behaviorConfiguration="tryBehavior"
        contract="ServiceAReference.IServiceA" name="NetTcpBinding_IServiceA" />
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MEXGET" >
          <!-- Add the following element to your service behavior configuration. -->
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="tryBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <customBinding>
        <binding name="MexTcp">
          <tcpTransport portSharingEnabled="true" />
        </binding>
      </customBinding>
      <netTcpBinding>
        <binding name="ReliableTCP" portSharingEnabled="true">
          <reliableSession enabled="true" />
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
          </security>
        </binding>
        <binding name="NetTcpBinding_IServiceA" receiveTimeout="00:05:00" sendTimeout="00:05:00" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
          <reliableSession enabled="true" />
          <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
          </security>
        </binding>
      </netTcpBinding>
      <mexTcpBinding>
        <binding name="MexTcp" />
      </mexTcpBinding>
    </bindings>
    <services>
      <service name="Test.Service.ServiceBImpl" behaviorConfiguration="MEXGET" >

        <endpoint address="mex"
                  binding="customBinding"
      bindingConfiguration="MexTcp"
                  contract="IMetadataExchange" />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
        <endpoint
             address="net.tcp://app-svr:10010/ServiceB/ServiceBImpl"
             binding="netTcpBinding" behaviorConfiguration="tryBehavior"
             bindingConfiguration="ReliableTCP"
             contract="Test.Service.ServiceB" />

        <host>
            <baseAddresses>
              <add baseAddress="http://app-svr:10011/ServiceB/ServiceBImpl" />
              <add baseAddress="net.tcp://app-svr:10010/ServiceB/ServiceBImpl" />
            </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true" />
  </system.webServer>
</configuration>

4 个答案:

答案 0 :(得分:1)

我为此创建了扩展方法。 一种用于 NetTcpBinding,一种用于 NetNamedPipeBinding。当然只能用于内部服务。时间也是如此,当夜间未使用默认服务时,它会在早上第一次调用时失败。

    public static void ActivateMaxValues(this NetTcpBinding b)
    {
        b.OpenTimeout = TimeSpan.FromHours(10);
        b.CloseTimeout = TimeSpan.FromMinutes(10);
        b.ReceiveTimeout = TimeSpan.FromHours(10);
        b.SendTimeout = TimeSpan.FromHours(10);

        b.MaxBufferSize = int.MaxValue;
        b.MaxReceivedMessageSize = int.MaxValue;

        b.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
        {
            MaxArrayLength = int.MaxValue,
            MaxBytesPerRead = int.MaxValue,
            MaxDepth = int.MaxValue,
            MaxNameTableCharCount = int.MaxValue,
            MaxStringContentLength = int.MaxValue
        };
    }

    public static void ActivateMaxValues(this NetNamedPipeBinding b)
    {
        b.TransactionFlow = true;

        b.OpenTimeout = TimeSpan.FromHours(1);
        b.CloseTimeout = TimeSpan.FromMinutes(10);
        b.ReceiveTimeout = TimeSpan.FromHours(1);
        b.SendTimeout = TimeSpan.FromHours(1);

        b.MaxBufferSize = int.MaxValue;
        b.MaxReceivedMessageSize = int.MaxValue;

        b.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
        {
            MaxArrayLength = int.MaxValue,
            MaxBytesPerRead = int.MaxValue,
            MaxDepth = int.MaxValue,
            MaxNameTableCharCount = int.MaxValue,
            MaxStringContentLength = int.MaxValue
        };
    }

答案 1 :(得分:0)

有一个解决方案here ......你应该尝试一下......

  1. 在服务和客户端配置中添加了这些行为。

    <behaviors>
     <endpointBehaviors>
      <behavior name="endpointBehavior">
    <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
     </endpointBehaviors>
    </behaviors>
    
  2. 在客户端和服务器配置中将这些值更新为最大大小。

    <binding name="tcpBinding" receiveTimeout="00:15:00" sendTimeout="00:15:00"  maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
      <security mode="None">
        <transport clientCredentialType="None" protectionLevel="None" />
        <message clientCredentialType="None" />
      </security>
    </binding>
    
  3. 希望它有所帮助。

答案 2 :(得分:0)

我也遇到了同样的错误,这是由于服务凭证错误或丢失引起的。由于您使用的是tcp绑定,请先创建绑定并正确设置安全性:

NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport)
        {
            CloseTimeout = TimeSpan.FromSeconds(timeoutInSeconds),
            OpenTimeout = TimeSpan.FromSeconds(timeoutInSeconds),
            SendTimeout = TimeSpan.FromSeconds(timeoutInSeconds),
            ReceiveTimeout = TimeSpan.FromSeconds(timeoutInSeconds)
        };

        binding.Security.Transport.ClientCredentialType = 
TcpClientCredentialType.Windows;
        binding.MaxReceivedMessageSize = int.MaxValue;
        binding.MaxBufferSize = int.MaxValue;
        binding.MaxBufferPoolSize = int.MaxValue;

创建客户端后,请确保设置用户名和密码:

var serviceClient = new MyServiceClient(binding, endpointYouDefine);
serviceClient.ClientCredentials.Windows.ClientCredential = new NetworkCredential("usernameInActiveDirectory", "passwordForTheADUser", "yourdomain.com");

此后不要对客户端执行任何其他操作。我已经看到了奇怪的行为,例如在分配凭据后以编程方式设置读取器配额,并且凭据被清除了。

答案 3 :(得分:0)

我设法通过在托管net.tcp Web服务的服务器中执行以下操作来解决此问题:

  1. 从services.msc重新启动NET TCP端口共享服务
  2. 以管理员身份打开命令提示符并运行IIS重置