开发者

WCF Multiple Apps using NetNamedPipe

开发者 https://www.devze.com 2023-02-17 17:23 出处:网络
I am trying to run multiple WCF Service hosting apps on the same Machine. I want to run multiple Applications - not multiple services in one application.

I am trying to run multiple WCF Service hosting apps on the same Machine.

I want to run multiple Applications - not multiple services in one application.

var host = new ServiceHost(typeof(MyClass1), new Uri[] { new Uri("net.pipe://localhost") });
host.AddServiceEndpoint(typeof(ISomeInte开发者_如何学编程rface),  new NetNamedPipeBinding(), "FOO");
host.Open();

I change "FOO" for every app, but still can not start multiple Services. Guess its pretty simple, but im stuck :(

Regards


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消