【问题标题】:Self-hosted WCF can connect to localhost but can't connect remote自托管 WCF 可以连接到 localhost 但无法远程连接
【发布时间】:2019-05-28 12:37:48
【问题描述】:

我有一个自托管的 C# WCF .Net 4.6.1 Windows 服务,它与另一个自托管的 WCF 服务进行通信。当两个服务都在同一台服务器上时,这可以正常工作。但是,当我将服务器移动到另一台计算机时,我收到此错误:

System.ServiceModel.CommunicationException:套接字连接已中止。这可能是由于处理您的消息时出错或远程主机超出接收超时,或者是潜在的网络资源问题引起的。两台计算机上都没有运行防火墙,我在使用http://192.168.1.129:6253/eTutorWcfService(在应用程序中使用 net.tcp)时得到响应。

客户app.config:

<bindings>
    <basicHttpBinding>
        <binding name="BasicHttpBinding_IeTutorMessage" />
    </basicHttpBinding>
    <netTcpBinding>
        <binding name="NetTcpBinding_IeTutorMessage" />
    </netTcpBinding>
</bindings>

<client>
    <endpoint name="BasicHttpBinding_IeTutorMessage" 
        address="http://localhost:6253/eTutorWcfService" 
        binding="basicHttpBinding"
        bindingConfiguration="BasicHttpBinding_IeTutorMessage" 
        contract="eTutorServiceReference.IeTutorMessage" />
    <endpoint name="NetTcpBinding_IeTutorMessage"
        address="net.tcp://localhost:6254/eTutorWcfService"
        binding="netTcpBinding"   
        bindingConfiguration="NetTcpBinding_IeTutorMessage"
        contract="eTutorServiceReference.IeTutorMessage" >
        <identity>
            <servicePrincipalName value = ""/>
        </identity>
    </endpoint>
</client>

服务器app.config:

<services>
    <service name="eTutorServer.eTutorWcfService" 
             behaviorConfiguration="myeTutorServiceBehavior">
        <host>
            <baseAddresses>
                <add baseAddress="http://localhost:6253/eTutorWcfService"/>
                <add baseAddress="net.tcp://localhost:6254/eTutorWcfService"/>
            </baseAddresses>
        </host>
        <endpoint  
            address="http://localhost:6253/eTutorWcfService" 
            binding="basicHttpBinding" 
            contract="eTutorServer.IeTutorMessage" />
        <endpoint 
            address="net.tcp://localhost:6254/eTutorWcfService" 
            binding="netTcpBinding" 
            contract="eTutorServer.IeTutorMessage" />
        <endpoint 
            address="mex" 
            binding="mexHttpBinding" 
            contract="IMetadataExchange"/>
        <endpoint 
            address="mex" 
            binding="mexTcpBinding" 
            contract="IMetadataExchange"/>
    </service>
</services>
<behaviors>
    <serviceBehaviors>
        <behavior name="myeTutorServiceBehavior">
            <serviceMetadata httpGetEnabled="true"/>
        </behavior>
    </serviceBehaviors>
</behaviors>

客户端代码:

EndpointAddress address = new EndpointAddress("net.tcp://" + eTutorServiceIp + ":6254/eTutorWcfService");
eTutorServiceReference.IeTutorMessageClient client = new eTutorServiceReference.IeTutorMessageClient("NetTcpBinding_IeTutorMessage", address);

try
{
    rtn = client.eTutorMessage(itm);
    client.Close();
}

当客户端尝试连接时,服务器的输出窗口会显示SecurityTokenValidationException,但我不确定该怎么做,或者它是否意味着相关。我确定这与安全性有关,但我不知道在哪里添加。

【问题讨论】:

  • 本地主机可能在两台电脑上配置不同。检查主机文件:C:\Windows\System32\Drivers\etc\hosts
  • 两台计算机上的 hosts 文件中都没有条目。
  • 使用像wireshark或fiddler这样的嗅探器,比较工作PC和非工作PC上的结果。查看 http 响应状态,看看您是否完成了 200。还要检查 TCP 以查看是否收到 [FIN],它会终止指示完成的 TCP 连接。
  • 也许您在客户端配置中输入的&lt;identity&gt; &lt;servicePrincipalName value = ""/&gt; &lt;/identity&gt;SecruityTokenValidationException 有关。我不知道,为什么它在配置中。
  • 在身份上,它会在某个时候使用“msi/steve”(主机名/用户)自动添加。我已经取出了值和节点,但得到了同样的错误。

标签: c# wcf wcf-security self-host-webapi


【解决方案1】:

首先,Nettcpbinding使用传输安全模式,默认使用windows凭据对客户端进行身份验证。
WCF throws exception that the server has rejected the client credentials, what is the default security mode for NetTCP in WCF
然后,当我们更改服务器配置并重新托管服务时,我们应该在调用它时重新生成客户端代理类。此外,由于默认生成 Localhost,我们可能需要在客户端配置中更改端点地址。

我可以忍受这个,但真的很想知道怎么做 没有安全性。

最后,当我们将安全性更改为无时,客户端无需提供凭据即可调用服务。我建议您重新托管服务并重新生成客户端代理类。我做了一个demo,希望对你有用。

服务器端(控制台应用程序)

class Program
    {
        static void Main(string[] args)
        {
            using (ServiceHost sh=new ServiceHost(typeof(MyService)))
            {
                sh.Opened += delegate
                {
                    Console.WriteLine("Service is ready......");
                };
                sh.Closed += delegate
                {
                    Console.WriteLine("Service is closed");
                };
                sh.Open();
                Console.ReadLine();
                sh.Close();

            }
        }
    }
    [ServiceContract]
    public interface IService
    {
        [OperationContract]
        string SayHello();
    }
    public class MyService : IService
    {
        public string SayHello()
        {
            return "Hello Stranger";
        }
}

App.config

<system.serviceModel>
    <services>
      <service behaviorConfiguration="Service1Behavior" name="VM1.MyService">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="mybinding" contract="VM1.IService" >
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:13007/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="mybinding">
          <security mode="None">
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Service1Behavior">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

客户端。

ServiceReference1.ServiceClient client = new ServiceReference1.ServiceClient();
            try
            {
                Console.WriteLine(client.SayHello());
            }
            catch (Exception)
            {

                throw;
            }

App.config

    <system.serviceModel>
        <bindings>
            <netTcpBinding>
                <binding name="NetTcpBinding_IService">
                    <security mode="None" />
                </binding>
            </netTcpBinding>
        </bindings>
        <client>
<!--we may need to change the generated endpoint address to autual server IP address.-->
            <endpoint address="net.tcp://10.157.13.69:13007/" binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_IService" contract="ServiceReference1.IService"
                name="NetTcpBinding_IService" />
        </client>
</system.serviceModel>

如果有什么我可以帮忙的,请随时告诉我。

【讨论】:

    【解决方案2】:

    我添加了以下代码,它可以工作:

    client.ClientCredentials.Windows.ClientCredential.UserName = runAs;
    client.ClientCredentials.Windows.ClientCredential.Password = runAsPassword;
    client.ClientCredentials.Windows.ClientCredential.Domain = runAsDomain;
    

    但是,我想在没有安全性的情况下执行此操作,因为它将放置在多个服务器上,而这些服务器都没有公共 IP。我试图添加到绑定,但在客户端它不是一个有效的节点,在服务器上,它会阻止服务启动。我尝试将以下代码添加到服务器,但它不会打开 ServiceHost:

    serviceHost.AddServiceEndpoint(typeof(eTutorWcfService), new NetTcpBinding(SecurityMode.None), "");
    

    我可以忍受这个,但我真的很想知道如何在没有安全性的情况下做到这一点。

    【讨论】:

      猜你喜欢
      • 2017-09-23
      • 1970-01-01
      • 2012-02-12
      • 2018-07-26
      • 1970-01-01
      • 2013-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多