1

I spent a whole day searching for a solution but I didn't solve my problem. As you can see from the title, I implemented a basic Web Socket using Asp.Net Core (3.1) framework and I deployed it on Azure (on a Web App service). I successfully make it works without the TLS protocol (so I think the ws has been configured in a good manner) but when I try to connect using wss I receive this errors on the client side:

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 

I tried to switch the "HTTPS Only" trigger on azure portal but it keep refusing any client to connect. enter image description here

Do you have any idea how to let wss works with Azure Web App? Do I need to configure a certificate? I read that azure provide a certificate if the user didn't have one. Thanks and regards


UPDATE: the code has been "copied" from this great Les Jackson's tutorial and is available on git hub at this precise address. The important part of the code is here:

/* THIS IS THE SERVER PART WHERE THE CONNECTION IS ACCEPTED */ 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); } } } } 
/* THIS IS THE STARTUP FILE*/ public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddWebSocketServerConnectionManager(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseWebSockets(); app.UseWebSocketServer(); } } 
/* THIS IS THE CLIENT (WRITTEN IN NET FULLFRAMEWORK) */ 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); 

Thank you for reading

8
  • Did you enable Web Sockets support in WebApp? Commented Jan 26, 2021 at 9:33
  • Yes I enabled it and without wss it works fine Commented Jan 26, 2021 at 9:36
  • 1
    May you please share your code that is related to WebSocket support? Commented Jan 26, 2021 at 9:43
  • I added the git hub repository link and I copy-and-paste the relevant part here Commented Jan 26, 2021 at 10:28
  • 1
    It works fine with the provided javascript client in the sample. I suspect the error happens because of TLS version when you connect from c# client with full framework. The screenshot of your Azure web app enforces min TLS 1.2. Set that in the .net client like below: ``` System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; ``` Commented Jan 28, 2021 at 8:38

1 Answer 1

2

As disussed over the comments, it works fine with the provided javascript client in the sample. The error in .net client happens because of TLS version when you connect from c# client with full framework. The screenshot of your Azure web app enforces min TLS 1.2. Set that in the .net client like below:

System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.