I'm creating a windows service that would communicate with another application. The problem I'm facing is that the NamedPipeServerStream
closes as soon as the client disconnects. I want that the server should remain open, so whenever the client application starts, it gets connected.
I don't want to use WCF, the messages would be small, so I just want to keep it simple using Named Pipes. Multiple clients can connect to the service. Is there a way to keep the server always running?
This is what I have so far:
var server = new NamedPipeServerStream("pipeeee123");
server.SetAccessControl(pipeSa);
server.WaitForConnection();
StreamReader reader = new StreamReader(server);
StreamWriter writer = new StreamWriter(server);
while (true)
{
var line = reader.ReadLine();
this.EventLog.WriteEntry("Got command " + line, EventLogEntryType.Information);
var resp = HandleCommand(line);
writer.WriteLine(resp);
writer.Flush();
}