远程主机已使用WCF关闭了现有连接

问题描述

我已经使用网络TCP绑定开发了WCF Windows服务。当wcf客户端和wcf服务都在域中(在两个不同的系统中)时,它可以正常工作

两个系统都在工作组中而不在域中时出错。

引发异常。来源:System.ServiceModel 4.0.0.0。异常详细信息:System.ServiceModel.CommunicationException:套接字连接已中止。这可能是由于处理您的消息时出错或远程主机超出了接收超时,或者是潜在的网络资源问题引起的。本地套接字超时为“ 04:59:59.795578​​1”。 ---> System.Net.sockets.socketException:远程主机强行关闭了现有连接

我尝试过的事情:

解决方法

首先,您需要在请求时添加Windows凭据,因为nettcpbinding默认为Windows身份验证:

    ServiceReference1.CalculatorClient calculatorClient = new ServiceReference1.CalculatorClient();
   calculatorClient.ClientCredentials.Windows.ClientCredential.UserName = "Administrator";
   calculatorClient.ClientCredentials.Windows.ClientCredential.Password = "Password";

如果添加凭据后仍然存在问题,则还需要添加一个混合端点:

    <endpoint address="mex"
    binding="mexTcpBinding"
    contract="IMetadataExchange"></endpoint>

这是我的App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>

    <system.serviceModel>
        <services>
            <service name="ConsoleApp5.CalculatorService" behaviorConfiguration="ServiceBehavior">
                <host>
                    <baseAddresses>
                        <add baseAddress="net.tcp://localhost:10234/GettingStarted/"/>
                    </baseAddresses>
                </host>
                <endpoint address="Test"
                          binding="netTcpBinding"
                          contract="ConsoleApp5.ICalculator"/>
                <endpoint address="mex"
                          binding="mexTcpBinding"
                          contract="IMetadataExchange"></endpoint>
            </service>
        </services>


        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceBehavior">
                    <serviceMetadata/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
    </system.serviceModel>

</configuration>

更新

将“模式”值设置为“消息”:

<binding name="Binding">
          <security mode="Message">
            <message clientCredentialType="Certificate" />
          </security>
        </binding>

这是我的App.config:

<?xml version="1.0"?>
<configuration>

  <system.serviceModel>
    <services>
      <service name="Microsoft.Samples.X509CertificateValidator.CalculatorService" behaviorConfiguration="CalculatorServiceBehavior">
        <!-- use host/baseAddresses to configure base address provided by host -->
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8001/servicemodelsamples/service"/>
          </baseAddresses>
        </host>
        <!-- use base address specified above,provide one endpoint -->
        <endpoint address="certificate" binding="netTcpBinding" bindingConfiguration="Binding" contract="Microsoft.Samples.X509CertificateValidator.ICalculator"/>
         
      </service>
    </services>

    <bindings>
        <netTcpBinding>
        <!-- X509 certificate binding -->
        <binding name="Binding">
          <security mode="Message">
            <message clientCredentialType="Certificate" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>

    <behaviors>
        
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceDebug includeExceptionDetailInFaults="true"/>
            <serviceMetadata/>
          <serviceCredentials>
            <!-- 
            The serviceCredentials behavior allows one to specify authentication constraints on client certificates.
            -->
            <clientCertificate>
             
                <authentication certificateValidationMode="None" revocationMode="NoCheck"/>
            </clientCertificate>
            <!-- 
            The serviceCredentials behavior allows one to define a service certificate.
            A service certificate is used by a client to authenticate the service and provide message protection.
            This configuration references the "localhost" certificate installed during the setup instructions.
            -->
            <serviceCertificate findValue="localhost" storeLocation="LocalMachine" storeName="My" x509FindType="FindBySubjectName"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

如果问题仍然存在,请随时告诉我。