【问题标题】:Self-hosted WCF service EndpointNotFoundException while using LAN使用 LAN 时自托管 WCF 服务 EndpointNotFoundException
【发布时间】:2013-02-17 13:10:55
【问题描述】:

我编写了 2 个简单的控制台应用程序,一个是托管 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 作为基地址。

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

任何想法我可能做错了什么?我试过 net.tcp 绑定,结果相同。

两台机器都禁用了 Windows 防火墙

【问题讨论】:

  • 系统防火墙呢?您是否为传入请求取消阻止 9102?

标签: c# wcf service wcf-endpoint


【解决方案1】:

当您将客户端或服务移动到另一台机器时,您没有提及更改服务地址。您正在使用的地址 - localhost:xxxx 只会在程序所在的机器上被识别,因此例如,如果您的服务在 Machine1 上并且您将客户端移动到 Machine2,则客户端将无法看到服务,因为它期望它在 Machine2 上。您可能在 IP 地址方面遇到了类似的问题,尽管它看起来更像是路由器地址。

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

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

【讨论】:

  • 我提到我已经尝试将主机 app.config 中的 baseAddress 设置为 http://localhost:9102http://192.168.1.11:9102 (这是主机的 IP 地址)。我没有尝试使用机器名称,但如果使用 ip 地址不起作用,我看不出它会如何工作?
猜你喜欢
  • 1970-01-01
  • 2013-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多