'WebSockets Server .NET Core - IsWebSocketRequest always returns false
I'm using what I consider some fairly "standard" code in .NET Core to implement a web socket server:
if (ctx.Request.Path == "/api/v1/sometest")
{
if (ctx.WebSockets.IsWebSocketRequest)
{
WebSocket wsSock = await ctx.WebSockets.AcceptWebSocketAsync();
But I can never establish a session because IsWebSocketRequest is always null.
I can see the Connection: Upgrade and Upgrade: websocket headers...
I know I must be missing something but unsure what... Scanned through github dotnet source code to see if I could figure it out that way but no luck finding a solution yet.
I'm using Windows 10 Kestrel VS Code .NET Core 3.1
Solution 1:[1]
I had AddWebSockets in Startup.ConfigureServices BUT I forgot to also include the middleware in Startup.Configure. Call app.UseWebSockets here to register the middleware in your chain.
Solution 2:[2]
According to this link app.UseWebSockets() should be placed right before app.UseEndpoints() in startup.cs .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | James Scott |
| Solution 2 | AmirHossein Rezaei |
