2
我有2個應用程序,我想通過.NET 3.5上的命名管道進行通信。它是一個請求/響應範例,數據以XML格式傳輸,讓我的生活更輕鬆。有一個監聽器應用程序和一個將請求發佈到管道的應用程序。我試圖用雙向管來做到這一點。我遇到的問題是對StreamReader.ReadToEnd()的調用似乎不會返回。我能做些什麼來解決這個問題?雙向命名管道問題
監聽器代碼
public Class Listener
{
private void ThreadFunc()
{
var pipe = new NamedPipeServerStream("GuideSrv.Pipe",PipeDirection.InOut);
var instream = new StreamReader(pipe);
var outstream = new StreamWriter(pipe);
while (true)
{
pipe.WaitForConnection();
var response = ProcessPipeRequest(instream);
outstream.Write(response.ToString());
pipe.Disconnect();
}
}
private XDocument ProcessPipeRequest(StreamReader stream)
{
var msg_in = stream.ReadToEnd(); // << This call doesnt return
var xml_in = XDocument.Parse(msg_in);
// do some stuff here
return new XDocument(....);
}
}
委託代碼
public XDocument doIt()
{
var xml = new XDocument(....);
using (var pipe = new NamedPipeClientStream(".", "GuideSrv.Pipe", PipeDirection.InOut))
{
using (var outstream = new StreamWriter(pipe))
using (var instream = new StreamReader(pipe))
{
pipe.Connect();
outstream.Write(xml.ToString());
xml = XDocument.Parse(instream.ReadToEnd());
}
}
return xml;
}