0

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

使用具有不同端口的 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因为不同的端点已经在监听这个端点。

4

1 回答 1

1

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

Net.pipe://localhost/mypipe

这里有一些参考,希望对你有用。
使用 App.Config 在 Windows 服务中使用 WCF 命名管道
如何修复在打开服务主机时发生的 AddressInUseException
如果有什么我可以帮助的,请随时告诉我。

于 2020-02-04T08:16:38.850 回答