Using Task.Run for IO bound work to avoid updating method signatures

45 views Asked by At

This method contains a bunch of Redis code, and is called in many places and is part of a deep method call stack:

public void Save()
{            
    Task.Run(async () =>
    {
        var transaction = cache.CreateTransaction();
        var _ = transaction.StringSetAsync();
        _ = transaction.SetAddAsync();      
        await transaction.ExecuteAsync();
    }).Wait();
}

I understand a thread pool thread will be used to do this work, and that the calling thread will be blocked via the Wait() call, and will be unable to take any more ASP.NET requests as it waits for the Redis code to finish.

Would this blocking be insignificant enough to warrant keeping the code how it is to avoid having to update the signature of all the methods that call this code to include async/await?

Also, because only transaction.ExecuteAsync() is awaited in the delegate, could transaction.ExecuteAsync() be run before the Tasks above it had completed?

0

There are 0 answers