Search code examples
asp.net-corewebsocketkestrel

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

  • 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.