【发布时间】: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:9102 和http://192.168.1.11:9102 作为基地址。
当在一台机器上打开主机和客户端应用程序时,它可以正常工作。当我将其中一个应用程序移动到另一台机器时,它在执行client.Open() 时返回System.EndpointNotFoundException。
任何想法我可能做错了什么?我试过 net.tcp 绑定,结果相同。
两台机器都禁用了 Windows 防火墙
【问题讨论】:
-
系统防火墙呢?您是否为传入请求取消阻止 9102?
标签: c# wcf service wcf-endpoint