我正在开发一个Windows服务应用程序,我遇到了有关命名管道服务器的问题。 服务应用程序具有命名管道服务,以便GUI与其通信 - 并且GUI具有命名管道客户端。
当我运行我的应用程序的控制台版本(相同的代码但在控制台项目中初始化)时,一切正常,但是当我运行应用程序的服务版本时(使用安装项目或使用VS unistallUtil.exe安装)我收到这个错误:
System.OperationCanceledException: The operation was canceled.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Pipes.NamedPipeServerStream.EndWaitForConnection(IAsyncResult asyncResult)
at Common.PipeCommunication.SomePipeServer.ClientConnected(IAsyncResult ar) in SomePipeServer.cs:line 80
启动服务时出现此错误,甚至没有尝试将客户端连接到服务。这是我的PipeServer类:
public class SomePipeServer
{
#region PRIVATE VARIABLES
private string _pipeName = "SomePipeServer";
private NamedPipeServerStream _pipeServer;
private IAsyncResult _connectRequest;
private byte[] oBuffer;
#endregion
#region PUBLIC VARIABLES
public event EventHandler MsgReceived;
#endregion
#region PUBLIC METHODS
public SomePipeServer()
{
oBuffer = new byte[4096];
}
public void Start()
{
try
{
_pipeServer = new NamedPipeServerStream(
_pipeName, PipeDirection.InOut, -1,
PipeTransmissionMode.Message,
PipeOptions.Asynchronous);
_connectRequest = _pipeServer.BeginWaitForConnection(
ClientConnected, null);
}
catch (Exception ex)
{
...
}
}
public void Stop()
{
try
{
_connectRequest = null;
if (_pipeServer == null)
return;
if (_pipeServer.IsConnected)
_pipeServer.Disconnect();
//_pipeServer.Close();
}
catch (Exception ex)
{
...
}
}
#endregion
#region PRIVATE METHODS
private void ClientConnected(IAsyncResult ar)
{
try
{
if (ar != null)
{
_pipeServer.EndWaitForConnection(ar); //line 80
_connectRequest = null;
_connectRequest = _pipeServer.BeginRead(oBuffer, 0, 4096,
ClientMessage, null);
}
}
catch (Exception ex)
{
...
}
}
private void ClientMessage(IAsyncResult ar)
{
try
{
if (ar != null)
{
_pipeServer.EndRead(ar);
string message = System.Text.ASCIIEncoding.ASCII.GetString(oBuffer).Trim(new char[] { '\0', ' ' });
message = message.Replace(Environment.NewLine, " ").TrimEnd();
OnReceive(message);
_connectRequest = null;
//_connectRequest = _pipeServer.BeginRead(oBuffer, 0, 4096,
// new AsyncCallback(ClientMessage), null);
//_pipeServer.Disconnect();
//_pipeServer.BeginWaitForConnection(ClientConnected, null);
Stop();
Start();
}
}
catch (Exception ex)
{
...
}
}
private void OnReceive(string in_msg)
{
if (MsgReceived != null)
{
MsgReceived(in_msg, EventArgs.Empty);
}
}
#endregion
}
答案 0 :(得分:0)
关于({3}}关于(我所理解的)与你有同样问题的讨论。
Windows将为已退出的线程完成所有重叠的I / O,并使用ERROR_OPERATION_ABORTED。
IAsyncResult并不一定意味着重叠的I / O,但它是一个 自然适合,它似乎是命名管道使用的方法。
在这种情况下,我建议让主线程创建 命名管道;这对于命名管道服务器来说是正常的(具有单独的管道服 线程只是为了创建命名管道而不是)。