我有NamedPipeClientStream连接到NamedPipeServerStream。他们交换了几条消息,然后关闭了NamedPipeClientStream,同时重新创建NamedPipeServerStream并继续侦听客户端管道。 (我无法使用异步服务器管道,所以这是一种狗钉子)
在从普通用户会话启动的客户端流中,客户端 - 服务器交互正常。
但是有一种情况是在Win7和win2008服务器上从会话0启动客户端管道。发生这种情况时,我在客户端流中出错:
“拒绝访问该路径”
问题是什么?怎么避免呢?
对不起,我无法告诉你有关异常的更多信息。只有我在日志中有此消息。我无法从零会话调试我的程序,可以吗?
服务器流代码:
PipeSecurity ps = new PipeSecurity();
System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.BuiltinUsersSid, null);
PipeAccessRule par = new PipeAccessRule(sid, PipeAccessRights.ReadWrite, System.Security.AccessControl.AccessControlType.Allow);
ps.AddAccessRule(par);
pipeClientConnection = new NamedPipeServerStream(General.PIPENAME, PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, General.BUFFERSIZE, General.BUFFERSIZE, ps);
Console.Write("Waiting for client connection...");
IAsyncResult result = pipeClientConnection.BeginWaitForConnection(OnPipeConnected, pipeClientConnection);
安全设置可能有问题吗?
和客户端代码:
using (NamedPipeClientStream pipeStream = new NamedPipeClientStream(".", General.PIPENAME, PipeDirection.InOut))
{
try
{
Console.WriteLine("Connecting with pipe...");
pipeStream.Connect(General.CONNECTIONTIMEOUT);
Console.WriteLine("Pipe connection established");
//..do something..
}
//...
}
服务器在LocalSystem下作为Windows服务启动。客户端 - 是一个简单的控制台应用程序。它是由LocalSystem服务启动的另一个应用程序启动的。
看起来问题出现在安全设置中:
System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.BuiltinUsersSid, null);
应该 :
System.Security.Principal.SecurityIdentifier sid = new System.Security.Principal.SecurityIdentifier(System.Security.Principal.WellKnownSidType.WorldSid, null);
我知道这个问题已经过时了,我只是遇到了这个问题,并认为我会添加它。
此错误还可以指示命名管道已在使用中。我有一个生产服务运行我尝试调试的相同可执行文件,并且在初始化该管道服务器时失败,因为它已被另一个进程使用。似乎错误应该对这种情况提供更多信息,但它就是它的本质。