简体   繁体   中英

Self-hosted WCF service EndpointNotFoundException while using LAN

I've written 2 simple console applications one that is hosting a wcf service and second that is wcf service client.

If I run both applications on one machine (host and client) it works ok, but if I try to move client or host to different machine I get EndpintNotFountException when trying to connect to the host.

This is my code of the service :

namespace ConsoleApplication7
{
    using System.ServiceModel;

    [ServiceContract]
    public interface ISimpleService
    {
        [OperationContract]
        void Register(string name);
    }
}

And implementing class:

namespace ConsoleApplication7
{
    public class SimpleService : ISimpleService
    {
        public void Register(string name)
        {
            DataBase.Save(name);
        }
    }
}

As you see it's very simple service :)

This is the code of host program :

using System;
using System.ServiceModel;

class Program
{
    static void Main(string[] args)
    {
        var service = new ServiceHost(typeof(SimpleService));
        service.Open();

        while (true)
        {
            Console.ReadLine();
            foreach (var s in DataBase.Get())
            {
                Console.WriteLine(s);
            }
        }
    }
}

It opens service and when enter pressed it will print the content of the data base. I know that it does not have Close() and other stuff but it is only for showing my problem.

Here is the app.config file :

  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name ="myBinding">
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
    <client />
    <services>
      <service name="ConsoleApplication7.SimpleService" behaviorConfiguration="ServiceBehavior">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="myBinding" contract="ConsoleApplication7.ISimpleService"/>
        <endpoint address="" binding="basicHttpBinding" contract="ConsoleApplication7.ISimpleService"/>
        <endpoint address ="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:9101"/>
            <add baseAddress="http://localhost:9102"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior" >
          <serviceMetadata httpGetEnabled="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

Client code:

using System;
using System.ServiceModel;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Click to open");
        Console.ReadLine();

        try
        {
            var service = new ServiceReference1.SimpleServiceClient(new BasicHttpBinding(), new EndpointAddress("http://192.168.1.11:9102/"));
            service.Open();
            service.Register("itsMe");
        }
        catch (Exception  e)
        {
            Console.WriteLine(e);
        }

        Console.ReadLine();
    }
}

Client app.config is created by visual studio by AddServiceReference option.

I host app.config file I tried both http://localhost:9102 and http://192.168.1.11:9102 as base addresses.

When open host and client applications on one machine it works fine. When I move one of those apps to other machine it returns System.EndpointNotFoundException when doing client.Open() .

Any ideas what I might be doing wrong ? I've tried with net.tcp binding and its the same result.

Windows firewall is disabled on both machines

You don't mention changing the service address when you move the client or service to a different machine. The address you're using - localhost:xxxx will only be recognized on the machine the program is on, so for example if you're service is on Machine1 and you move the client to Machine2, the client will not be able to see the service as it will expect it to be on Machine2. You're probably running into a similar problem with the IP address, though that looks more like a router address.

I would suggest updating the service's config file to use the machine name (for example, http://machine1:9102 ), and using that address to access the service from the client. For example:

var service = new ServiceReference1.SimpleServiceClient(new BasicHttpBinding(), new EndpointAddress("http://machine1:9102/"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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