You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C# Windows服务中NamedPipeServerStream无法重连问题排查

问题原因及解决方案

问题根源

  1. IsConnected属性不可靠:服务端依赖pipeServer.IsConnected判断连接状态,但该属性无法实时反映客户端的主动断开行为。当客户端关闭连接后,ReadLineAsync会返回null,但此时IsConnected可能仍为true,导致服务端卡在循环中无法退出,无法创建新管道等待下一次连接。
  2. 空引用异常未处理:当ReadLineAsync返回null时,直接调用message.Split(' ')会抛出空引用异常,进一步阻断服务端的正常循环流程。

修复后的服务端代码

private async Task StartPipeServer()
{
    while (true)
    {
        PipeSecurity pipeSecurity = new PipeSecurity();
        pipeSecurity.AddAccessRule(new PipeAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), PipeAccessRights.ReadWrite, AccessControlType.Allow));
        using (var pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 1024, 1024, pipeSecurity))
        {
            await pipeServer.WaitForConnectionAsync();
            using (StreamReader reader = new StreamReader(pipeServer, Encoding.UTF8))
            using (StreamWriter writer = new StreamWriter(pipeServer, Encoding.UTF8) { AutoFlush = true })
            {
                string message;
                // 通过ReadLineAsync返回值判断连接状态,null表示客户端断开
                while ((message = await reader.ReadLineAsync()) != null)
                {
                    // 过滤空消息,避免空引用异常
                    if (string.IsNullOrWhiteSpace(message))
                        continue;
                    
                    string[] CastiPrikazu = message.Split(' ');
                    switch (CastiPrikazu[0])
                    {
                        case "Ahoj":
                            writer.WriteLine("ahoj");
                            break;
                    }
                }
            }
        }
    }
}

修改说明

  • 替换循环判断逻辑:用ReadLineAsync的返回值直接判断连接状态,当返回null时立即退出内层循环,释放当前管道实例,让服务端回到外层循环等待新连接。
  • 增加空消息校验:避免空字符串或空白消息导致的Split操作异常,提升服务端稳定性。

内容的提问来源于stack exchange,提问作者Radim Krejčiřík

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 20:35:02