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



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

我的虚拟机带有Windows服务,可容纳WCF。我想从运行该虚拟机的本地计算机上运行的客户端与此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。我可以从本地机器中使用它。我不使用任何端口转发。

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

<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中的防火墙阻止了我的连接。原始评论:

因此,您需要指定VM的IP地址。但是首先,您需要确保可以访问它。尝试连接Whit Telnet。如果您无法连接,那么第一件事就是要做的就是在VM的防火墙(新规则 -> select port-> next-> next-> Enter WCF服务端口 ->完成)中添加入站规则。防火墙最有可能阻止您在VM WCF端口上的连接。 - Reniuz

相关内容

  • 没有找到相关文章

最新更新