【问题标题】:WCF Same Service Contract in several Server Instances多个服务器实例中的 WCF 相同服务合同
【发布时间】:2020-05-18 13:45:54
【问题描述】:

我正在尝试创建同一 WCF 服务器 (NetNamedPipes) 的多个实例(多次启动应用程序),但在启动第二个实例时遇到问题...服务器实例使用不同的管道名称和端点名称。我使用了来自here 的示例,仅通过启动参数设置端点和管道名称。但是在第二种情况下,我收到一条错误消息,指出存在错误状态,因此无法打开服务主机。

使用不同端口的 Http 绑定是可行的,但我想坚持使用命名管道。

服务器:

[ServiceContract]
public interface IServiceContract
{
    [OperationContract]
    string Operation(string value);
}

class Program : IServiceContract
{
    static void Main(string[] args)
    {
        Console.WriteLine($"Pipe: {args[0]}");
        Console.WriteLine($"Endpoint: {args[1]}");

        ServiceHost sh = new ServiceHost(typeof(Program), new Uri($"net.pipe://{args[0]}"));

        sh.AddServiceEndpoint(typeof(IServiceContract), new NetNamedPipeBinding(), args[1]);

        sh.Open();

        Console.ReadLine();
    }

    public string Operation(string value)
    {
        return value.ToUpper();
    }
}

错误消息:AddressAlreadyInUseException,因为另一个端点已经在监听这个端点。

【问题讨论】:

  • 看来问题与管道地址中“localhost”的使用有关。如果 URI 如下$"net.pipe://localhost/{args[0]}",则可以启动第二个应用程序。但不知道为什么......

标签: c# wcf netnamedpipebinding


【解决方案1】:

由于NetNamedPipeBinding机制服务只能接收来自同一台机器的调用,所以管道名是管道地址的唯一标识字符串。我们只能在同一台机器上打开一个命名管道,因此两个命名管道地址不能在同一台机器上共享相同的管道名称。
此外,正如你提到的,NetNamedPipeBinding 形式的URI

Net.pipe://localhost/mypipe

这里有一些参考,希望对你有用。
WCF Named Pipe in Windows Service using App.Config
How to fix AddressInUseException that occurs whenever service host is opened
如果有什么我可以帮忙的,请随时告诉我。

【讨论】:

  • 感谢您的意见!正如我在评论中所写,该问题的解决方案是使用像net.pipe://localhost/somepipe 这样的管道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-20
  • 2011-08-17
  • 1970-01-01
相关资源
最近更新 更多