如何在 VM 中使用托管在 windows 服务中的 WCF

How to use WCF hosted in windows service in VM

我对 WCF 及其配置没有足够的经验,我不知道如何解决这个问题。所以我很抱歉这个可能是愚蠢的问题。

我的虚拟机具有托管 WCF 的 windows 服务。我想从客户端 运行ning 与此虚拟机 运行 的本地计算机上的此 WCF 通信。我可以 运行 本地服务没有问题,但是当涉及到与虚拟机上的服务通信时,我真的很难正确配置它。我想在不使用 IIS 的情况下使用 TCP 连接。

我尝试指定 VM 地址、备用地址和端点,但均未成功。你能告诉我什么是正确的配置吗?

<system.serviceModel>

<bindings>
  <netTcpBinding>
    <binding name="NetTcpBindingConfiguration">
      <security mode="None" />
    </binding>
  </netTcpBinding>
</bindings>

<services>
  <service behaviorConfiguration="WCFHostBehaviour" name="TestRunnerWCF.Service">
    <endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBindingConfiguration"
      name="WCFHostNetTcpEndpoint" contract="TestRunnerWCF.IService" />
    <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
      name="WCFHostMexTcpEndpoint" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://192.168.10.25/Service" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="WCFHostBehaviour">
      <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

更新 1: 我不知道如何在我的客户中添加服务参考。如果我在我的本地机器上使用 localhost 我 运行 服务,这是不需要的。

Hyper-V 中的虚拟机 运行,地址为 192.168.10.25。我可以从本地机器 ping 通它。我不使用任何端口转发。

我可以 运行 使用此配置的虚拟机上的客户端:

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="WCFHostNetTcpEndpoint">
                <security mode="None" />
            </binding>
        </netTcpBinding>
    </bindings>
    <client>
        <endpoint address="net.tcp://192.168.10.25/Service" binding="netTcpBinding"
            bindingConfiguration="WCFHostNetTcpEndpoint" contract="ServiceReference1.IService"
            name="WCFHostNetTcpEndpoint">
        </endpoint>
    </client>
</system.serviceModel>

但是当我尝试 运行 本地计算机上的客户端时,我无法连接到我的虚拟机。

Error: Cannot obtain Metadata from net.tcp://192.168.10.25/Service If this 
is a Windows (R) Communication Foundation service to which you have access,
please check that you have enabled metadata publishing at the specified 
address.  For help enabling metadata publishing, please refer to the MSDN 
documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata 
Exchange Error    URI: net.tcp://192.168.10.25/Service    Metadata contains 
a reference that cannot be resolved: 'net.tcp://192.168.10.25/Service'.   
Could not connect to net.tcp://192.168.10.25/Service. The connection attempt 
lasted for a time span of 00:00:21.0324561. TCP error code 10060: A 
connection attempt failed because the connected party did not properly 
respond after a period of time, or established connection failed because 
connected host has failed to respond 192.168.10.25:808.     A connection 
attempt failed because the connected party did not properly respond after a 
period of time, or established connection failed because connected host has 
failed to respond 192.168.10.25:808

正如 Reniuz 指出的那样,VM 中的防火墙阻止了我的连接。 原评论:

So you need to specify VM's ip address. But first you need to ensure you can access it. Try to connect whit telnet . If you can't connect, first thing what you can do is to add inbound rule in VM's firewall (new rule-> select port->next-> enter wcf service port -> finish). Most likely the firewall is blocking in coming connection on your VM wcf port. – Reniuz