Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 895
enzi Avatar asked Jan 06 '23 09:01

enzi


1 Answers

ConfigureAwait does not remove the sync context. It merely causes the task completion continuation to not use the current sync context.

It is always safe to use if the rest of the current async method does not depend on the sync context. Emphasis on method as opposed to thread.

like image 165
usr Avatar answered Jan 30 '23 08:01

usr