【问题标题】:An existing connection was closed by the remote host with WCF远程主机使用 WCF 关闭了现有连接
【发布时间】:2020-11-30 09:49:23
【问题描述】:

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

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

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

我尝试过的事情:

  • 关闭防火墙
  • 检查端口
  • 增加超时时间

【问题讨论】:

  • 我的第一个想法是检查并确保客户端和服务器都支持相同版本的 TLS。这篇文章可能会有所帮助,尤其是 John Wu 的回答...stackoverflow.com/questions/45382254/…
  • 您好,问题解决了吗?
  • 没有问题还没有解决

标签: c# wcf


【解决方案1】:

首先,请求时需要添加windows凭据,因为nettcpbinding默认为windows认证:

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

如果添加凭据后仍有问题,还需要添加 mex 端点:

    <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>

更新

将 Mode 值设置为 Message:

<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>

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

【讨论】:

  • 我正在使用证书进行身份验证,但仍然存在问题
猜你喜欢
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 2011-01-06
  • 2013-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多