【发布时间】:2017-08-11 03:42:11
【问题描述】:
我正在托管我的服务:
using (ServiceHost sHost = new ServiceHost(typeof(Class1)))
{
sHost.Open();
}
// sHost.Open();
Console.WriteLine("Service Started....");
Console.ReadLine();
并尝试使用通道工厂的服务:
ChannelFactory<IService1> factory = new ChannelFactory<IService1>("");
var proxy = factory.CreateChannel();
var result = proxy.DoProcessing();
我可以看到服务已启动,使用端点,但是当我尝试创建通道时,出现以下错误。
测试方法TestService.Test.UnitTest1.TestMethod1抛出异常: System.ServiceModel.EndpointNotFoundException:无法连接到 net.tcp://127.0.0.1/TestService。连接尝试持续了 00:00:01.0017385 的时间跨度。 TCP 错误代码 10061: 无法建立连接,因为目标机器主动拒绝它 127.0.0.1:808。 ---> System.Net.Sockets.SocketException: No connection could be made because the target machine主动拒绝它127.0.0.1:808
这是我的服务配置:
<services>
<service name="Service1.ClassLibrary.Class1">
<endpoint
address="net.tcp://127.0.0.1/TestService"
binding="netTcpBinding"
contract="Service1.Interface.IService1" />
</service>
</services>
</system.serviceModel>
客户端配置:
<client>
<endpoint address="net.tcp://127.0.0.1/TestService" binding="netTcpBinding"
contract="Service1.Interface.IService1" />
</client>
【问题讨论】:
-
net 库将不允许您使用 ip 地址为 127.0.0.1 的 connect() 方法。您必须使用计算机 IP 地址或名称。您可以使用 127.0.0.1 收听。
-
@jdweng,我尝试使用 localhost ,但同样的错误
-
某些计算机的 localhost 定义为 127.0.0.1,这将不起作用。