我有一个在启动过程中启动许多NamedPipeServerStreams的服务:
Thread[] serverThreads = new Thread[numThreads];
for (int i = 0; i < numThreads; i++)
{
serverThreads[i] = new Thread(ServerThread);
serverThreads[i].Start(i);
}
...
private void ServerThread(object data)
{
int buffer = 32768;
PipeSecurity bouncer = new PipeSecurity();
bouncer.AddAccessRule(new PipeAccessRule("PipeServer", PipeAccessRights.FullControl, AccessControlType.Allow));
bouncer.AddAccessRule(new PipeAccessRule("PipeClient", PipeAccessRights.ReadWrite, AccessControlType.Allow));
try
{
NamedPipeServerStream pipeServer = new NamedPipeServerStream
(
"TestPipe",
PipeDirection.InOut,
NamedPipeServerStream.MaxAllowedServerInstances,
PipeTransmissionMode.Message,
PipeOptions.Asynchronous,
buffer,
buffer,
bouncer
);
Console.WriteLine("Server " + data + " listening for TestPipe...");
pipeServer.WaitForConnection();
Console.WriteLine("Server " + data + " got connection from client.");
}
catch (Exception ex)
{
Console.WriteLine("Server " + data + " failed: " + ex.Message);
}
}
[当此代码由不是“ PipeServer”成员的用户执行时,由于某种原因,仍会创建第一个服务器,开始监听并接受连接:
服务器0监听TestPipe ...服务器1失败:对路径的访问被拒绝。服务器3失败:对路径的访问被拒绝。服务器2失败:对路径的访问被拒绝。服务器0从客户端获得连接。
当代码是“ PipeServer”成员的用户执行时,所有线程都会启动并接受连接:
服务器0监听TestPipe ...服务器1监听TestPipe ...服务器2正在侦听TestPipe ...服务器3监听TestPipe ...服务器0从客户端获得连接。服务器1从客户端获得连接。服务器2从客户端获得连接。服务器3从客户端获得连接。
为什么当不允许访问时,PipeSecurity
为什么不能在所有线程上工作?
您是否具有名为“ PipeServer”和“ PipeClient”的用户帐户设置?我怀疑您不是要将这些值作为第一个参数传递给PipeAccessRule。该参数是用户帐户,因此请尝试类似“ USERS”和“ SYSTEM”的内容。
PipeAccessRule pr1 = new PipeAccessRule("USERS", PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance, System.Security.AccessControl.AccessControlType.Allow);
PipeAccessRule pr2 = new PipeAccessRule("SYSTEM", PipeAccessRights.FullControl, System.Security.AccessControl.AccessControlType.Allow);