WCF Multiple Apps using NetNamedPipe

前端 未结 2 1657
时光取名叫无心
时光取名叫无心 2021-02-06 04:12

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

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-06 04:47

    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.

提交回复
热议问题