代码之家  ›  专栏  ›  技术社区  ›  JamesStuddart

NamedPipes在windows服务中引发异常

  •  2
  • JamesStuddart  · 技术社区  · 14 年前

    我创建了一个windows服务,允许通过namedpipes进行通信。

    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 我也犯了同样的错误。

    这是线程的代码:

    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();
    

    任何帮助都会很好的。

    下面是运行侦听器的代码:

    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;
        }
    }
    

    找到异常消息:所有管道实例都忙。

    3 回复  |  直到 7 年前
        1
  •  2
  •   JamesStuddart    14 年前

    结果发现我的整个实现是有缺陷的,我删除了很多代码并重新编写了它,它工作了。我从in ListenForMessage()中删除了代码,并将其替换为来自ServerThread()的代码,然后还将服务调用此代码的方式从计时器更改为线程。而且成功了。

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

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

        2
  •  1
  •   Richard    14 年前

    命名管道名称的格式必须为:

    \\.\pipe\pipename

    http://msdn.microsoft.com/en-us/library/aa365150(VS.85).aspx


    编辑:关于进一步的检查(看 NamedPipeServerStream 示例)可能不是这样。因此需要查看异常的全部详细信息: Message

        3
  •  0
  •   Don R.    14 年前

    您没有包含异常。消息,但根据NamedPipeServerStream的MSDN页,IOException是“已超过服务器实例的最大数目”。这是您得到的异常吗?