2010-10-29 60 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