WCF Multiple Apps using NetNamedPipe

↘锁芯ラ 提交于 2019-12-03 03:06:48

Approaching it like this will do what you want, I believe:

string relativeUriPart = GetUniquePartFromConfigOfThisApplicationInstance();
var host = new ServiceHost(typeof(MyClass1)); // No base addresses specified
host.AddServiceEndpoint(
    typeof(ISomeInterface),  
    new NetNamedPipeBinding(), 
    "net.pipe://localhost/" + relativeUriPart); // Specify absolute URI for endpoint
host.Open();

This is because, if you specify a base address which uses the net.pipe scheme, it is this base address which is used to derive the pipe name used by the listener [see edit below], and this is the same in each application instance, so only the first application's listener can create the pipe - the others fail as you have noted.

Using the absolute URI at the endpoint level, with no base address, the listener is created with a pipe name derived [see edit below] from the full absolute URI, which is different in each application instance, and so each application's listener can create its own distinct pipe without any problem.


EDIT: To be more precise, the pipe name itself is not derived from the service address at all - it is a GUID which changes each time the service is opened. What is derived from the service address is the name of a shared memory object via which the actual name of the pipe is published to prospective clients. See here for more details.

If you need to create service hosts for different service contracts as show here:

...    
host1 = new SeviceHost(typeof(MyClass1, ...);
host2 = new ServiceHost(typeof(MyClass2, ...);
...

then you do need to use different base addresses for each new ServiceHost as Mathew's answer suggests. If all of your service hosts are for the same typeof(MyClass1) then you may just need to create multiple endpoints for that same service. Each endpoint could be for a different interface (i.e. ISomeInterface1, ISomeInterface2, ...) in that service.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!