c# - NamedPipes 在 Windows 服务中抛出异常

标签 c# windows-services named-pipes

我创建了一个 Windows 服务,允许通过命名管道进行通信。

当我编写一些单元测试来启动管道并测试通信时,此代码运行良好,但现在我在 Windows 服务中安装了相同的代码,但出现以下错误:

Exception Info: System.IO.IOException
Stack:
       at System.IO.__Error.WinIOError(Int32, System.String)
       at System.IO.Pipes.NamedPipeServerStream.Create(System.String, System.IO.Pipes.PipeDirection, Int32, System.IO.Pipes.PipeTransmissionMode, System.IO.Pipes.PipeOptions, Int32, Int32, System.IO.Pipes.PipeAccessRights, SECURITY_ATTRIBUTES)
       at System.IO.Pipes.NamedPipeServerStream..ctor(System.String, System.IO.Pipes.PipeDirection, Int32, System.IO.Pipes.PipeTransmissionMode, System.IO.Pipes.PipeOptions, Int32, Int32, System.IO.Pipes.PipeSecurity, System.IO.HandleInheritability, System.IO.Pipes.PipeAccessRights)
       at System.IO.Pipes.NamedPipeServerStream..ctor(System.String, System.IO.Pipes.PipeDirection, Int32, System.IO.Pipes.PipeTransmissionMode,     
System.IO.Pipes.PipeOptions, Int32, Int32, System.IO.Pipes.PipeSecurity)
   at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart(System.Object)

现在我做了一些谷歌搜索,并在 stackoverflow 中找到了这篇文章> POST但我实现了这个(除了 ps.AddAccessRule(pa); 因为它没有提到 was pa was) 我得到了同样的错误。

这是我的线程代码:

var pipeSecurity = new PipeSecurity();
pipeSecurity.AddAccessRule(new PipeAccessRule("Users", PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance, AccessControlType.Allow));
pipeSecurity.AddAccessRule(new PipeAccessRule("CREATOR OWNER", PipeAccessRights.FullControl, AccessControlType.Allow));
pipeSecurity.AddAccessRule(new PipeAccessRule("SYSTEM", PipeAccessRights.FullControl, AccessControlType.Allow));

var pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, numThreads, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 1024, 1024, pipeSecurity);    

pipeServer.WaitForConnection();

任何帮助都会很棒。

好的,这是运行监听器的代码:

Windows 服务:

public static System.Timers.Timer Timer = new System.Timers.Timer();

public void Start()
{
    Timer.Elapsed += (HeartBeat);
    //Timer.Interval = 100; //Live
    Timer.Interval = 2000; //Debug
    Timer.Start();
}

public void Stop()
{
    Timer.Stop();
}

private static void HeartBeat(object sender, ElapsedEventArgs e)
{
    //listen for a message
    ListenForMessage();

}

监听器代码:

private const String pipeName = "StackOVerFlowPipeCode";
private const int numThreads = 10;

public static void ListenForMessage()
{
    int i;
    var servers = new Thread[numThreads];

    for (i = 0; i < numThreads; i++)
    {
        servers[i] = new Thread(ServerThread);
        servers[i].Start();
    }

    Thread.Sleep(250);

    while (i > 0)
    {
        for (var j = 0; j < numThreads; j++)
        {
            if (servers[j] == null) continue;
            if (!servers[j].Join(250)) continue;

            servers[j] = null;

            i--;    // decrement the thread watch count
        }
    }
}

private static void ServerThread(object data)
{
    try
    {
        var pipeServer = new NamedPipeServerStream(pipeName, PipeDirection.InOut, numThreads);

        pipeServer.WaitForConnection();

        var ss = new StreamString(pipeServer);
        ss.WriteString(pipeName);

        var message = ss.ReadString();

        //DO STUFF HERE WITH MESSAGE

        pipeServer.Close();
    }
    catch (Exception ex)
    {
        //CRY LIKE A BABY WHO LOST HIS TEDDY
        throw ex;
    }
}

发现异常消息:所有管道实例都忙。

最佳答案

事实证明我的整个实现是有缺陷的,我删除了很多代码并重写了它并且它起作用了。我删除了 ListenForMessage() 中的代码,并将其替换为 ServerThread() 中的代码,然后还更改了服务调用此方法的方式,将其更改为计时器中的线程。它起作用了。

好吧,收到消息后的代码(如上面的//Do Stuff 所示)确实可以工作,但至少这个任务已经完成了。

注意:永远不要只是复制和过去代码并认为这是理所当然的,始终阅读并理解它。

关于c# - NamedPipes 在 Windows 服务中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4051048/

相关文章:

c - 命名管道终止 session

ubuntu-12.04 - Bind9 记录到命名管道

c# - IOrganisationService.Retrieve 记录不存在

c# - 带回调的双工服务

C#/WPF : Toolkit DataGrid - Transpose rows and columns

c++ - 如何从服务获取用户桌面的路径?

c# - CancellationToken 不适用于 WaitForConnectionAsync

c# - 更改 winforms 应用程序的字体系列和大小

wcf - 优雅地终止 WCF 服务 - 完成所有打开的 session 并限制新 session

c# - 使用 Windows 服务接收 MSMQ 消息