Closed princefishthrower closed 2 years ago
I CAN connect if I use the
app.Use
way of doing things with websockets in myStartup.cs
, but I would greatly prefer containing all my websocket logic in controller instead.To Reproduce
- Create a brand new .NET project with
dotnet new webapi -n WebSocketsTutorial
- add
app.UseWebSockets();
toStartup.cs
Can you show your Startup.cs for both of these aproaches? One issue with UseWebSockets is that it needs to be placed early in the pipeline.
@Tratcher - Ah, that was it! Working with the boilerplate Startup.cs
, I just tried all possible locations of app.UseWebSockets()
, it turns out that app.UseEndpoints()
was the culprit - as long as app.UseWebSockets()
occurs before that call, it works! Now the question becomes, is this intentional or expected behavior due to the internals of .NET or just a straight-up bug? Either way, it should be mentioned somewhere on the documentation page:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-5.0
I've got a pull request ready with an info block below the app.UseWebsockets()
code snippet that would have saved me from this problem:
This is intentional, UseWebSockets must run before any components that want to use websockets. In this case you're doing WebSockets in an MVC controller which runs in the UseEndpoints step.
I agree the doc could be clearer about this. I'm going to transfer this to the docs repo.
@serpent5 this looks fun, let me know if you want it.
@Rick-Anderson Sure. Can you add it to Serp
? I'll keep track of things in there. Feel free to prioritise them accordingly. I'll speak up if there's something I'd rather not do.
Closing as an example of using a controller has been added since this discussion was opened.
Web sockets created with _host.GetTestServer().CreateWebSocketClient()
work well even if UseWebSockets()
is called after UseEndpoints()
.
Describe the bug
I am unable to accept WebSocket connections in a controller endpoint. The endpoint logic is entered when I debug, but the value of
HttpContext.WebSockets.IsWebSocketRequest
is ALWAYS false.ws://, wss://, calling from chrome, calling from firefox, it doesn't matter.
I CAN connect if I use the
app.Use
way of doing things with websockets in myStartup.cs
, but I would greatly prefer containing all my websocket logic in controller instead.To Reproduce
Create a brand new .NET project with
dotnet new webapi -n WebSocketsTutorial
add
app.UseWebSockets();
toStartup.cs
Create a new controller named
WebSocketsController
paste this code into that controller:
Go into a browser (tested so far on firefox and chrome) and try this:
let webSocket = new WebSocket('wss://localhost:5001/ws');
It will fail with the 400 code, as written in the controller handler.
Can anyone reproduce, or have I lost my mind? What am I missing? Source's I've used:
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-5.0
https://sahansera.dev/understanding-websockets-with-aspnetcore-5/
Further technical details
dotnet --info
:Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.