Search code examples
asp.net-coremiddlewaresignalr-hub

How to create a middleware that is invoked on each method on the hub while using signalR with websockets


I'm using signalR hub with Websockets and I need to create a middleware that will be invoked on each method on the hub. I used to work with context accessor but it only has the http request which has the first request to connect to the hub and not the actual method that is called on the hub.


Solution

  • I'm using signalR hub with Websockets and I need to create a middleware that will be invoked on each method on the hub.

    Hub filters in ASP.NET Core 5.0 or later are very similar to middleware, and it can help run custom logic before and after hub method(s) are invoked by client(s).

    For details about ASP.NET Core SignalR hub filters, please check this doc: https://learn.microsoft.com/en-us/aspnet/core/signalr/hub-filters?view=aspnetcore-6.0