Search code examples
c#wcfvirtual-machinenet.tcpwcf-endpoint

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. So I am sorry for this, possibly dumb, question.

I have virtual machine with windows service that hosts WCF. I want to communicate with this WCF from client running on local machine that runs this virtual machine. 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.

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. 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. I can ping it from local machine. 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

Solution

  • As Reniuz point out, firewall in VM was blocking my connection. Original comment:

    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