我们使用ServiceHost已有数年之久,可以在应用程序之间进行本地通信。
我们打开一个net.pipe:net.pipe://localhost/OurCompanyName/
在Windows 10补丁 KB4480966 到达之前,该方法一直有效。
这是打开服务主机的代码:
NetNamedPipeBinding binding = new NetNamedPipeBinding();
binding.MaxConnections = 100;
binding.MaxReceivedMessageSize = int.MaxValue;
binding.Security.Mode = NetNamedPipeSecurityMode.Transport;
binding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
//binding.ReceiveTimeout = new TimeSpan(0, 0, 10, 0);
this.serviceHost = new ServiceHost(typeof(LocalService), new Uri[] { new Uri(hostBaseAddress) });
this.serviceHost.AddServiceEndpoint(typeof(ILocalService), binding, localServiceEndPointAddress);
foreach (ServiceEndpoint ep in this.serviceHost.Description.Endpoints)
{
foreach (OperationDescription op in ep.Contract.Operations)
{
DataContractSerializerOperationBehavior dataContractBehavior =
op.Behaviors.Find<DataContractSerializerOperationBehavior>()
as DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
{
dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
}
}
}
this.serviceHost.Open();
自从提到补丁以来,我们得到了这个异常:
无法侦听管道名称“ net.pipe:// localhost / OurCompanyName /”,因为另一个管道端点已经在侦听该名称。