【问题标题】:System.ServiceModel.EndpointNotFoundException: Could not connect to net.tcp://127.0.0.1/TestServiceSystem.ServiceModel.EndpointNotFoundException:无法连接到 net.tcp://127.0.0.1/TestService
【发布时间】: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,这将不起作用。

标签: c# wcf sockets tcp


【解决方案1】:

问题在于:

using (ServiceHost sHost = new ServiceHost(typeof(Class1)))
                {
                    sHost.Open();
                }

using bloick 自动关闭服务主机,因此对客户端不可用。

相反,这很完美:

 ServiceHost sHost = new ServiceHost(typeof(Class1));
    sHost.Open();
    Console.WriteLine("Service Started....");
   //Keeps Servece Open
    Console.ReadLine();
 sHost.Close()

【讨论】:

    猜你喜欢
    • 2020-11-19
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    • 2015-07-31
    • 1970-01-01
    • 2018-08-03
    • 2019-01-09
    相关资源
    最近更新 更多