2010-10-29 57 views
1

我创建了一个允许通过命名管道进行通信的Windows服务。命名管道在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) 

现在我做了一些谷歌搜索,但是我实现了这个(除了ps.AddAccessRule(pa);因为没有提及pa是) ,我得到了同样的错误。

这是我对线程的代码:

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

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

回答

1

原来我的整个实现是有缺陷的,我删除了很多代码,并重写了它,它工作。我从ListenForMessage()中删除了代码,并将其替换为ServerThread()中的代码,然后还改变了服务如何将它调用到定时器的线程。它的工作。

很好,接收到消息后的代码(如上面的Do Stuff所示)确实有效,但至少此任务已完成。

注意:永远不要复制和过去的代码,并将其视为理所当然的总是阅读并理解它。

+0

你应该对你的问题做一个编辑,除非你打算在这里发布你的实际代码,然后把这个标记为答案,以便其他的SO'ers知道你做了什么不同的事情,这样他们也可以学习。只是FYI。 – jcolebrand 2010-10-29 14:26:52

+0

上面的评论解释了我是如何做到的,与原始问题中的代码完全相同,但谢谢。 – JamesStuddart 2010-10-29 16:09:55

0

您没有包含exception.Message,但根据NamedPipeServerStream的MSDN页面,IOException是针对“已超过服务器实例的最大数量”。这是你得到的例外吗?

如果是这样,numThreads设置为什么,它有可能你有另一个应用程序的实例运行? (例如,单元测试你正在试验?)

+0

我确实包括例外?我设置的最大数量是10,并且管道甚至没有被创建,它在我开始服务后立即爆炸 – JamesStuddart 2010-10-29 11:05:39

+0

您确定您的测试应用程序不再运行吗?并且,您包含异常的类型(IOException),但不包含消息。 – 2010-10-29 11:33:57

+0

我现在已经添加了我正在使用的代码,是否可以让心跳一直在旋转排列器? – JamesStuddart 2010-10-29 11:47:45