Search code examples
c#signalrsignalr-hub

Can I use `ConfigureAwait(false)` in SignalR hubs, or does SignalR infrastructure rely on SynchronizationContext?


I have some hub methods that perform I/O operations which I want to call asynchronously.

Is it safe to use ConfigureAwait(false) in my hub methods, or does SignalR require a captured SynchronizationContext for request info or something like that?

    public async Task<Response> Save() {
        // do some prep work
        var response = await SaveThingsAsync().ConfigureAwait(false);
        // do something with response
        return response;
    }

To clarify: my code does not require thread culture or anything else stored in the SynchronizationContext.
My concern is that the SignalR server code might store information there, such as Client Ids, request infos or the like, which it may not be able to access if the async method is continued from a different thread.

When I tested my method it appeared to work fine, but that doesn't proof anything necessarily.


Solution

  • SignalR does not depend on SynchronizationContext. It is commonly self-hosted in Win32 services (and console apps, for testing), where there is no SynchronizationContext.

    See also this SignalR issue.