Partager via


Using IPC Channels and with Multiple Users

When I'm working with a new technology or concept, I find that an ultra simple, bare bones code sample or app is one of the most useful things to use for getting started. When it comes to understanding how to work with IPC channels (named pipes), the best example I've see is at Ohad's blog at https://weblogs.asp.net/israelio/archive/2005/01/04/346180.aspx

It is mentioned that config files can be used instead of manually coding up everything. In some cases writing up the code is necessary. For example, the project I'm working on can be run by multiple users and each user has both a IPC client as well as an app domain containing the IPC server. This means that multiple named pipes need to be created, and the channel names must be unique per user so that multiple users can run the program. (If the same pipe name is used for everyone then an AccessDenied remoting exception will be generated).

From MSDN: "You cannot register a channel that listens on a specific port more than once. Even though channels are registered on a per-application-domain basis, different application domains on the same machine cannot register the same channel listening on the same port." https://msdn.microsoft.com/en-us/library/dkfd3wha(VS.80).aspx

To keep the pipe names unique per user, I simply add the current user domain and name. For example,

IDictionary props = new Hashtable();
props["portName"] = "MyAppClientPort" + Environment.UserDomainName + Environment.UserName;
props["name"] = "MyAppClientName" + Environment.UserDomainName + Environment.UserName;

IpcChannel ipcChannel = new IpcChannel(props, null, null);
ChannelServices.RegisterChannel(ipcChannel, false);