简体   繁体   中英

WCF Same Service Contract in several Server Instances

I am trying to create multiple instances of the same WCF server (NetNamedPipes) (starting application several times) but running into a problem on starting the second instance... The server instances are using different pipe names and endpoint names. I used the example from here , only setting the endpoint and pipe name via start up arguments. But on the second instance, I get an error message that there is a faulty state and therefore the servicehost cannot be opened.

Using Http binding with different ports works, but I would like to stick with named pipes.

Server:

[ServiceContract]
public interface IServiceContract
{
    [OperationContract]
    string Operation(string value);
}

class Program : IServiceContract
{
    static void Main(string[] args)
    {
        Console.WriteLine($"Pipe: {args[0]}");
        Console.WriteLine($"Endpoint: {args[1]}");

        ServiceHost sh = new ServiceHost(typeof(Program), new Uri($"net.pipe://{args[0]}"));

        sh.AddServiceEndpoint(typeof(IServiceContract), new NetNamedPipeBinding(), args[1]);

        sh.Open();

        Console.ReadLine();
    }

    public string Operation(string value)
    {
        return value.ToUpper();
    }
}

Error message: AddressAlreadyInUseException because a different endpoint is already listening to this endpoint.

Due to the fact that NetNamedPipeBinding mechanism service can only receive calls from the same machine, the pipe name is the unique identifier string for the pipe address. We can only open one named pipe on the same machine, so two named pipe addresses cannot share the same pipe name on the same machine.
Besides, as you mentioned, the URI in the form of NetNamedPipeBinding is

Net.pipe://localhost/mypipe

Here are some references, wish it is useful to you.
WCF Named Pipe in Windows Service using App.Config
How to fix AddressInUseException that occurs whenever service host is opened
Feel free to let me know if there is anything I can help with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM