简体   繁体   English

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

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

I don't have enough experience with WCF and its configuration and I don't know how to approach this problem. 我对WCF及其配置没有足够的经验,也不知道如何解决此问题。 So I am sorry for this, possibly dumb, question. 因此,对于这个可能很愚蠢的问题,我感到抱歉。

I have virtual machine with windows service that hosts WCF. 我有带有托管WCF的Windows服务的虚拟机。 I want to communicate with this WCF from client running on local machine that runs this virtual machine. 我想从运行此虚拟机的本地计算机上运行的客户端与此WCF通信。 I can run the service locally with no problem, but when it comes to communicate with service on virtual machine I have really hard time to configure it properly. 我可以毫无问题地在本地运行该服务,但是在虚拟机上与服务进行通信时,我真的很难正确配置它。 I want to use TCP connection without the use of IIS. 我想使用TCP连接而不使用IIS。

I tried to specify address of VM, alternate addresses and endpoints with no success. 我尝试指定虚拟机的地址,备用地址和端点,但均未成功。 Can you advice me what is correct configuration? 您能告诉我什么是正确的配置吗?

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

UPDATE 1: I don't know how to add service reference in my client. 更新1:我不知道如何在我的客户端中添加服务引用。 If I use localhost I run service on my local machine and that is not desired. 如果使用本地主机,则在本地计算机上运行服务,这是不希望的。

Virtual machine runs in Hyper-V and have address 192.168.10.25. 虚拟机在Hyper-V中运行,地址为192.168.10.25。 I can ping it from local machine. 我可以从本地计算机ping它。 I don't use any port forwarding. 我不使用任何端口转发。

I can run client on virtual machine with this config: 我可以使用以下配置在虚拟机上运行客户端:

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

But when I try to run client on local machine I can't connect to my virtual machine. 但是,当我尝试在本地计算机上运行客户端时,无法连接到我的虚拟机。

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

As Reniuz point out, firewall in VM was blocking my connection. 正如Reniuz所指出的,VM中的防火墙阻止了我的连接。 Original comment: 原始评论:

So you need to specify VM's ip address. 因此,您需要指定VM的IP地址。 But first you need to ensure you can access it. 但是首先,您需要确保可以访问它。 Try to connect whit telnet . 尝试连接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). 如果无法连接,首先要做的是在VM的防火墙中添加入站规则(新规则->选择端口->下一步->输入wcf服务端口->完成)。 Most likely the firewall is blocking in coming connection on your VM wcf port. 防火墙很可能在您的VM wcf端口上阻止即将到来的连接。 – Reniuz –雷尼兹

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

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