未找到 WCF 命名管道终结点

WCF Named Pipes Endpoint not found

简单来说,我们需要通过 WCF 命名管道进行 outproc 通信。在 dev harness 中,客户端和服务组件的应用程序都通过 IOC 在同一个可执行文件中实例化。

服务主机:

/// <summary>
/// Default constructor
/// </summary>
public OpaRuntimeServiceHost(string serviceName, string hostAddress)
{
    _serviceHost = new ServiceHost(typeof(OpaRuntimeService), new Uri[] {
        new Uri(string.Format("net.pipe://{0}/opa/{1}", hostAddress, serviceName))
    });
    _serviceHost.AddServiceEndpoint(typeof(IOpaRuntimeService), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), serviceName);
    _serviceHost.Open();
}

客户:

/// <summary>
/// Default constructor
/// </summary>
/// <param name="hostAddress"></param>
/// <param name="serviceName"></param>
public OpaRuntimeServiceClient(string serviceName, string hostAddress)
    : base(new ServiceEndpoint(ContractDescription.GetContract(typeof(IOpaRuntimeService)),
    new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), new EndpointAddress(string.Format("net.pipe://{0}/opa/{1}", hostAddress, serviceName))))
{

}

两者都构建成功,但当客户端调用服务时,它会生成此错误:

There was no endpoint listening at net.pipe://localhost/opa/runtime that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

不幸的是,没有内部异常。根据其他问题,我确保 Net.Pipe 侦听器服务是 运行。 Visual Studio 是 运行 提升的权限。

环境是 Windows 10 上的 VS2015 或 Windows 7 上的 VS2012。

我错过了什么吗?

我相信对 AddServiceEndpoint 的调用需要端点的地址(根据 MSDN documentation)。在您的示例代码中,您似乎只传递了服务名称。

我找到了一些做类似事情的示例代码。但是,在我的示例中,我是从 ServiceHost:

派生的
public class CustomServiceHost : ServiceHost
{
    public CustomServiceHost() : base(
        new CustomService(),
        new[] { new Uri(string.Format("net.pipe://localhost/{0}", typeof(ICustomService).FullName)) })
    {

    }

    protected override void ApplyConfiguration()
    {
        base.ApplyConfiguration();

        foreach (var baseAddress in BaseAddresses)
        {
            AddServiceEndpoint(typeof(ICustomService), new NetNamedPipeBinding(), baseAddress);
        }
    }
}

明白了。服务名称在服务主机设置中被使用了两次,因此当它应该包含这样的内容时,会生成类似 net.pipe://localhost/opa/runtime/runtime 的内容:net.pipe://localhost/opa/runtime。谢谢你的橡皮鸭。