You need to enable JavaScript to run this app. Azure Web App托管ASP.NET Core WebSocket启用TLS后WSS连接失败如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Azure Web App托管ASP.NET Core WebSocket启用TLS后WSS连接失败如何解决

部署在Azure Web App的ASP.NET Core 3.1 WebSocket服务WSS连接异常问题

我花费了一整天时间排查解决方案,始终未能解决问题。基于ASP.NET Core 3.1框架实现了基础WebSocket功能,并部署到Azure Web App服务上:

  • 不启用TLS协议时WebSocket可正常运行,因此可确认WebSocket基础配置逻辑没有问题。
  • 尝试使用wss协议连接时,客户端抛出如下错误:
System.Net.WebSockets.WebSocketException : Unable to connect to the remote server
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send.
System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host

我已在Azure门户尝试开启「仅HTTPS」开关,但服务仍然拒绝所有客户端的连接请求。(附Azure配置界面截图)

我需要明确两个问题:要怎么配置才能让WSS在Azure Web App上正常运行?是否需要自行配置证书?我了解到如果用户没有自有证书,Azure会提供默认证书。


补充说明

本次实现的代码参考了公开的WebSocket开发实践,核心代码片段如下:

  1. 服务端接收WebSocket连接的中间件逻辑
/* 服务端接受WebSocket连接的核心逻辑 */
namespace WebSocketServer.Middleware
{
    public class WebSocketServerMiddleware
    {
        private readonly RequestDelegate _next;
        private WebSocketServerConnectionManager _manager;

        public WebSocketServerMiddleware(RequestDelegate next, WebSocketServerConnectionManager manager)
        {
            _next = next;
            _manager = manager;
        }

        public async Task InvokeAsync(HttpContext context)
        {
            if (context.WebSockets.IsWebSocketRequest)
            {
                WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();

                await Receive(webSocket, async (result, buffer) =>
                {
                    if (result.MessageType == WebSocketMessageType.Text)
                    {
                        Console.WriteLine("Receive->Text");
                        return;
                    }
                    else if (result.MessageType == WebSocketMessageType.Close)
                    {
                        await sock.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
                        return;
                    }
                });
            }
            else
            {
                await _next(context);
            }
        }
    }
}
  1. 应用启动配置逻辑
/* 应用Startup启动配置 */
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddWebSocketServerConnectionManager();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseWebSockets();
        app.UseWebSocketServer();
    }
}
  1. 基于.NET Full Framework实现的客户端连接逻辑
Console.Write("Connecting....");
var cts = new CancellationTokenSource();
var socket = new ClientWebSocket();
string wsUri = "wss://testingwebsocket123123.azurewebsites.net";

await socket.ConnectAsync(new Uri(wsUri), cts.Token);
Console.WriteLine(socket.State);

await Task.Factory.StartNew(
    async () =>
    {
        var rcvBytes = new byte[1024 * 1024];
        var rcvBuffer = new ArraySegment<byte>(rcvBytes);
        while (true)
        {
            WebSocketReceiveResult rcvResult = await socket.ReceiveAsync(rcvBuffer, cts.Token);
            byte[] msgBytes = rcvBuffer.Skip(rcvBuffer.Offset).Take(rcvResult.Count).ToArray();
            string rcvMsg = Encoding.UTF8.GetString(msgBytes);
            Console.WriteLine("Received: {0}", rcvMsg);
        }
    }, cts.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default);

内容的提问来源于stack exchange,提问作者brian enno

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.06 15:06:03