1

I'm trying to migrate my old WebSocket server to ASP.NET Core 3.1. Problem is that even minimum example is not working - when connecting server via web socket on debug, no code is executed and websocket connect timeouts.

I just create empty ASP.NET Core 3.1 project from template and add: - app.UseWebSockets(); - app.Use - see code below I also comment out default Routing and UseEndpoints.

Startup code:

 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseWebSockets(); app.Use(async (context, next) => { if (context.WebSockets.IsWebSocketRequest) { WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync(); await Echo(context, webSocket); } else { await context.Response.WriteAsync("Not a websocket request"); //context.Response.StatusCode = 400; } }); //app.UseRouting(); //app.UseEndpoints(endpoints => //{ // endpoints.MapGet("/", async context => // { // //int a = 0; // await context.Response.WriteAsync("Hello World!"); // }); //}); } private async Task Echo(HttpContext context, WebSocket webSocket) { var buffer = new byte[1024 * 4]; WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); while (!result.CloseStatus.HasValue) { await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None); result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); } await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); } 

When I connect to this server via browser/normal http request, I get correct response "Not a websocket request". But when I try to connect via WebSocket, no user code is hit. In ASP .NET Core 2.2 code works.

Do I have to specify somewhere to accept WebSockets?

5
  • when I try to connect via WebSocket, no user code is hit Based on my test, WebSockets supports well in .NET Core 3.0. Please share the code snippet you are using to establish connection and exchange message, so that we can troubleshoot the issue better. Commented Dec 12, 2019 at 3:05
  • @FeiHan I was trying to make minimal example based on that doc. When I download sample referenced from that document it works. But that is Asp Net Core 2.x. I'm connecting to my server via Firefox Simple WebSocket client or simple c# websocket-sharp code. Both works with 2.x sample. Commented Dec 12, 2019 at 5:40
  • 1
    Did a test using Firefix Simple WebSocket Client plugin, and which works well. I'd like to know which one you try to connect, ws:// or wss://? Commented Dec 12, 2019 at 6:24
  • 1
    That's it! Using wss:// and everything works! Property in launchSettings.json - "sslPort": decides whenever secure websocket connection is REQUIRED ("Configure for HTTPS" checkbox when creating a new project) Commented Dec 12, 2019 at 8:04
  • Glad to hear the issue has been solved. Before we start to establish connection(s), we should check the server URL and port first. Commented Dec 12, 2019 at 9:32

2 Answers 2

3

That's it! Using wss:// and everything works!

As I mentioned, WebSockets supports well in ASP.NET Core 3.0 and 3.1.

Besides, as we discussed in comments, we should check the server URL and port before we start to establish connection(s).

Using wss://

enter image description here

Using ws://

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

Not an answer for this particular case. But it happened to me to be unable to connect to wss after refactoring. Try a GET request to your websoket endpoint. It might reveal that you are missing dependencies for injection in your constructor, like a repository or a service.

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.