使用LAN时自托管WCF服务EndpointNotFoundException

时间:2013-02-17 13:10:56

标签: c# wcf service wcf-endpoint

我写了两个简单的控制台应用程序,一个托管一个wcf服务,另一个是wcf服务客户端。

如果我在一台计算机(主机和客户端)上运行这两个应用程序,它可以正常运行,但是如果我尝试将客户端或主机移动到不同的计算机,则在尝试连接到主机时会得到EndpintNotFountException

这是我的服务代码:

namespace ConsoleApplication7
{
    using System.ServiceModel;

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

实施课程:

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

如您所见,服务非常简单:)

这是主程序的代码:

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);
            }
        }
    }
}

它打开服务,当输入按下时,它将打印数据库的内容。我知道它没有Close()和其他东西,但它只是为了显示我的问题。

以下是app.config文件:

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

客户代码:

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();
    }
}

客户app.config由visual studio通过AddServiceReference选项创建。

我托管app.config文件我尝试了http://localhost:9102http://192.168.1.11:9102作为基地址。

在一台机器上打开主机和客户端应用程序时,它可以正常工作。当我将其中一个应用程序移动到其他计算机时,它会在执行System.EndpointNotFoundException时返回client.Open()

任何想法我可能做错了什么?我尝试使用net.tcp绑定并获得相同的结果。

两台计算机上都禁用了Windows防火墙

1 个答案:

答案 0 :(得分:0)

将客户端或服务移动到其他计算机时,不提及更改服务地址。您正在使用的地址 - localhost:xxxx将仅在程序所在的计算机上被识别,因此,例如,如果您的服务在Machine1上并且您将客户端移动到Machine2,则客户端将无法看到服务,因为它将在Machine2上。你可能遇到了与IP地址类似的问题,虽然看起来更像是路由器地址。

我建议更新服务的配置文件以使用计算机名称(例如,http://machine1:9102),并使用该地址从客户端访问服务。例如:

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