【Azure Relay】记录使用Azure Relay在通信中遇见侦听器(Listener)或发送端(Sender)断开时的异常日志 (Hybrid Connection by Websocket)

简介: Azure Relay 是一种中继服务,用于在无法直连的客户端与服务端间建立安全通信通道。其核心为 HybridConnectionListener(服务端)与 HybridConnectionClient(客户端),基于 WebSocket 实现双向消息传递。异常断开时,双方均记录 `RelayException`,提示“远程方未完成关闭握手”,仅 TrackingId 格式可区分断开方。

问题描述

使用Azure Relay服务,可以帮助服务器与客户端无法直接访问的情况下,提供牵线搭桥中转的沟通方式,俗称“中继”。

它是一种单对单的通信方式,需要对服务端和客户端代码有一些改动才可以,需要定义服务端侦听器(Listener)来接受消息,客户端(Caller)来发送消息。

  • 服务端侦听器(Listener): 使用 HybridConnectionListener 对象来初始化对象
  • 客户端发送者(Caller):使用 HybridConnectionClient 对象来初始化对象

 

Azure Relay的工作原理如下

原理图:

流程说明:

  1. Listening client sends a listening request to the Azure Relay service. The Azure load balancer routes the request to one of the gateway nodes.
  2. The Azure Relay service creates a relay in the gateway store.
  3. Sending client sends a request to connect to the listening service.
  4. The gateway that receives the request looks up for the relay in the gateway store.
  5. The gateway forwards the connection request to the right gateway mentioned in the gateway store.
  6. The gateway sends a request to the listening client for it to create a temporary channel to the gateway node that's closest to the sending client.
  7. The listening client creates a temporary channel to the gateway that's closest to the sending client. Now that the connection is established between clients via a gateway, the clients can exchange messages with each other.
  8. The gateway forwards any messages from the listening client to the sending client.
  9. The gateway forwards any messages from the sending client to the listening client.

基于以上的理解,当Listener 或 Caller 都突然中断的时候, Listener端会记录什么异常日志呢?Caller端会记录什么异常日志呢?

 

问题解答

第一部分:Caller端异常退出的情况, Listener会记录什么日志呢?

结果

在Listener的日志中,会输出如下的错误消息。

如果不对异常进行处理,会导致整个进程奔溃,Listener退出。

Exception in processing connection:

Microsoft.Azure.Relay.RelayException: The remote party closed the WebSocket connection without completing the close handshake.

TrackingId:xxx-xx-xx-xx-xxxx4_G8_G9,

Address:sb://xxxxxxxx.servicebus.chinacloudapi.cn/xxxxxxxt01,

Timestamp:5/17/2026 8:48:56 AM


---> System.Net.WebSockets.WebSocketException: The remote party closed the WebSocket connection without completing the close handshake.

  at System.Net.WebSockets.WebSocketBase.WebSocketOperation.<Process>d__19.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

  at System.Net.WebSockets.WebSocketBase.<ReceiveAsyncCore>d__45.MoveNext()

--- End of stack trace from previous location where exception was thrown ---



实验

分别启动Listener 和 Caller,在正常通信两次后,直接关闭Caller窗口

 

 

第二部分:Listener端异常退出的情况, Caller会记录什么日志呢?

结果

在Caller的日志中,会输出如下的错误消息。

Microsoft.Azure.Relay.RelayException:

The remote party closed the WebSocket connection without completing the close handshake.

TrackingId:xxxx-x-x-xx-xxxx5e79e2e,

Address:sb://xxxxxx.servicebus.chinacloudapi.cn/xxxxxxst01, Timestamp:5/17/2026 9:29:11 AM

---> System.Net.WebSockets.WebSocketException: The remote party closed the WebSocket connection without completing the close handshake.

  at System.Net.WebSockets.WebSocketBase.WebSocketOperation.<Process>d__19.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

  at System.Net.WebSockets.WebSocketBase.<ReceiveAsyncCore>d__45.MoveNext()


实验

分别启动Listener 和 Caller,在正常通信后,直接关闭Listener窗口

对比两种情况下的日志,都是 Microsoft.Azure.Relay.RelayException: The remote party closed the WebSocket connection without completing the close handshake.

 

微小的区别在异常消息中的TrackingId格式不同:

  • 如果是Caller异常断开,TrackingId中会携带 _GX_GX 标识,比如: TrackingId:xxx-xx-xx-xx-xxxx4_G8_G9
  • 如果是Listener异常断开,TrackingId中不会携带,它的值就是普通的guid字符串,如TrackingId:xxx-xx-xx-xx-xxxxxx0a5

以上内容,供您参考。

 

参考资料

什么是 Azure 中继?https://docs.azure.cn/zh-cn/azure-relay/relay-what-is-it

开始在 .NET 中使用中继混合连接 WebSocket : https://docs.azure.cn/zh-cn/azure-relay/relay-hybrid-connections-dotnet-get-started

 



当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

相关文章
|
18天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23535 12
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
6天前
|
Shell API 开发工具
Claude Code 快速上手指南(新手友好版)
AI编程工具卷疯啦!Claude Code凭借任务驱动+终端原生的特性,成了开发者的效率搭子。本文从安装、登录、切换国产模型到常用命令,手把手带新手快速上手,全程避坑,30分钟独立用起来。
1761 10
|
3天前
|
人工智能 开发工具 iOS开发
Claude Code 新手完全上手指南:安装、国产模型配置与常用命令全解
Claude Code 是一款运行在终端环境中的 AI 编程助手,能够直接在命令行中完成代码生成、项目分析、文件修改、命令执行、Git 管理等开发全流程工作。它最大的特点是**任务驱动、终端原生、轻量高效、多模型兼容**,无需图形界面、不依赖 IDE 插件,能够深度融入开发者日常工作流。
1176 1
|
5天前
|
人工智能 JSON BI
DeepSeek V4-Pro 接入 Claude Code 完全实战:体验、测试与关键避坑指南
Claude Code 作为当前主流的 AI 编程辅助工具,凭借强大的代码理解、工程执行与自动化能力深受开发者喜爱,但原生模型的使用成本相对较高。为了在保持能力的同时进一步降低开销,不少开发者开始寻找兼容度高、价格更友好的替代模型。DeepSeek V4 系列的发布带来了新的选择,该系列包含 V4-Pro 与 V4-Flash 两款模型,并提供了与 Anthropic 完全兼容的 API 接口,理论上只需简单修改配置,即可让 Claude Code 无缝切换为 DeepSeek 引擎。
1248 0
|
12天前
|
人工智能 缓存 Shell
Claude Code 全攻略:命令大全 + 实战工作流(完整版)
Claude Code 是一款运行在终端环境下的 AI 编码助手,能够直接在项目目录中理解代码结构、编辑文件、执行命令、执行开发计划,并支持持久化记忆、上下文压缩、后台任务、多模型切换等专业能力。对于日常开发、项目维护、快速重构、代码审查等场景,它可以大幅减少手动操作、提升编码效率。本文从常用命令、界面模式、核心指令、记忆机制、图片处理、进阶工作流等维度完整说明,帮助开发者快速上手并稳定使用。
2795 4
|
3天前
|
人工智能 JSON BI
Claude Code 搭配 DeepSeek V4-Pro 完整测评:超越 Claude Sonnet 4.5,低成本高效能背后的真实表现
Claude Code 凭借强大的代码理解、工程执行与自动化任务能力,成为开发者广泛使用的 AI 编程工具。但原生模型的调用成本较高,长期高频使用会带来明显开销。DeepSeek V4 系列模型发布后,凭借优秀的代码能力与兼容 Anthropic 协议的 API 接口,成为替代原生模型的高性价比选择。本文完整记录将 Claude Code 对接 DeepSeek V4-Pro 的配置流程、真实任务测试效果、优势亮点与必须注意的使用限制,为开发者提供可直接落地的参考方案。
803 1
|
5天前
|
人工智能 Linux API
hermes agent 安装教程:安装优化 + 模型配置 + 工具启用指南
Hermes Agent 是 Nous Research 于 2026 年发布的开源自主进化 AI 智能体框架(MIT 协议,Python 编写)。它通过任务沉淀技能、持久化记忆、原生多工具集成与并行子智能体,实现“越用越强”。支持 Linux/macOS/WSL2,安装便捷,面向个人与企业的新一代私有化 AI 助手。