WCF Multiple Apps using NetNamedPipe
Asked Answered
A

2

9

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(ISomeInterface),  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

Anasarca answered 16/3, 2011 at 13:48 Comment(0)
I
8

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.

Igenia answered 17/3, 2011 at 10:17 Comment(0)
Q
0

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.

Quillen answered 16/3, 2011 at 14:19 Comment(5)
I want to run multiple Applications not multiple services in one application.Anasarca
Could you please define "application"? If your application uses multiple service contracts (interfaces), a single WCF service with a single host can be used as long as you add an endpoint for each implemented interface. If you have created multiple WCF services that each implement a different interface then you must use a separate ServiceHost for each service.Quillen
application = process. In other words: I want a couple of processes to host the same service on different "addresses".Anasarca
I don't have much experience with processes in .NET but it seems to me that you need to a ServiceHost object per process with different base address for each process as Matthew suggested. Instantiating multiple processes sounds like overkill from a WCF perspective because you can easily configure a single service to listen on different endpoint addresses. I'll see if I can find a code example tomorrow. Good luck!Quillen
It's not about hosting services, its about IPC on a single machine.Anasarca

© 2022 - 2024 — McMap. All rights reserved.