NetTcpBinding 问题

NetTcpBinding problems

我使用以下代码创建了一个 windows 服务:

protected override void OnStart(string[] args)
{
    var localAddress = new Uri("net.pipe://localhost");
    var netExposedAddress = new Uri("net.tcp://localhost:8523");

    var addressCollection = new Uri[] { localAddress, netExposedAddress };
    var type = typeof(AnalCron);

    host = new ServiceHost(type, addressCollection);

    host.AddServiceEndpoint(typeof(IAnalCron),
    new NetNamedPipeBinding(),
    "PipeReverse");

    host.AddServiceEndpoint(typeof(IAnalCron),
    new NetTcpBinding(SecurityMode.None),
    "TcpReverse");

    host.Open();

    TheLoop();
}

并且我已经编写了测试以查看部署后我是否可以与该服务通信:

[TestMethod]
public void TestRemote()
{
    var binding = new NetTcpBinding(SecurityMode.None);
    var epAddrs = new EndpointAddress("net.tcp://192.168.201.27:8523/TcpReverse");

    var pipeFactory =
      new ChannelFactory<IAnalCron>(
        binding,
        epAddrs);

    IAnalCron pipeProxy =
    pipeFactory.CreateChannel();

    var x = pipeProxy.ReverseString("bla");

    Assert.AreEqual("alb", x);
}

[TestMethod]
public void TestLocal()
{
    var binding = new NetNamedPipeBinding();
    var epAddrs = new EndpointAddress("net.pipe://localhost/PipeReverse");

    var pipeFactory =
      new ChannelFactory<IAnalCron>(
        binding,
        epAddrs);

    IAnalCron pipeProxy =
      pipeFactory.CreateChannel();

    var x = pipeProxy.ReverseString("bla");

    Assert.AreEqual("alb", x);
}

当我从本地机器使用服务时没有出现问题,但是当我使用上面的代码访问网络上的远程机器时,出现以下错误:

Could not connect to net.tcp://192.168.201.27:8523/TcpReverse. The connection attempt lasted for a time span of 00:00:21.0231021. TCP error code 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.201.27:8523.

我尝试输入机器名而不是 IP 地址,但没有成功。 .config 文件中没有配置,我想在代码中设置所有内容。知道我做错了什么吗?

正如@stuartd 所指出的,我的防火墙配置有问题。当我将服务添加到例外列表时,通信顺利进行。