繁体   English   中英

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

[英]How to use WCF hosted in windows service in VM

我对WCF及其配置没有足够的经验,也不知道如何解决此问题。 因此,对于这个可能很愚蠢的问题,我感到抱歉。

我有带有托管WCF的Windows服务的虚拟机。 我想从运行此虚拟机的本地计算机上运行的客户端与此WCF通信。 我可以毫无问题地在本地运行该服务,但是在虚拟机上与服务进行通信时,我真的很难正确配置它。 我想使用TCP连接而不使用IIS。

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

<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:我不知道如何在我的客户端中添加服务引用。 如果使用本地主机,则在本地计算机上运行服务,这是不希望的。

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

因此,您需要指定VM的IP地址。 但是首先,您需要确保可以访问它。 尝试连接telnet。 如果无法连接,首先要做的是在VM的防火墙中添加入站规则(新规则->选择端口->下一步->输入wcf服务端口->完成)。 防火墙很可能在您的VM wcf端口上阻止即将到来的连接。 –雷尼兹

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM