0

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.

royalTS
  • 603
  • 4
  • 22
  • it seems that the problem is related to the usage of "localhost" in the pipe address. If the URI is as follows `$"net.pipe://localhost/{args[0]}"` the second application can be started. But not sure why... – royalTS Feb 03 '20 at 08:54

1 Answers1

1

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.

Abraham Qian
  • 7,117
  • 1
  • 8
  • 22
  • Thank you for your input! The solution for the problem was, as written in my comment, to use a pipe like `net.pipe://localhost/somepipe`. – royalTS Feb 04 '20 at 10:19