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