I know, that the question of async/await is old as hills, but nevertheless, it would be great if someone help me with the following:
Preconditions: .NetFramework. Legacy.
- API controller with synchronous action methods.
- The action methods call some private methods, which create objects of
ISomething
and, - using a
public void Run(ISomething input)
method from another class (the instance of the class is created in the constructor of the controller), - send
ISomething
objects to another part of the application via an asynchronous library like_lib.Send(ISomething input)
.
- The action methods call some private methods, which create objects of
Since public void Run(ISomething input)
is synchronous and the methods of the library are asynchronous, there is an adapter under the hood of public void Run(ISomething input)
.
The whole picture looks like this:
// _lib.Send(ISomething input) returns Task here and performs IO operation
public void Run(ISomething input)
{
RunAsync(() => _lib.Send(ISomething input));
}
private void RunAsync(Func<Task> input)
{
var result = Task.Run(() =>
{
input.Invoke();
}).ConfigureAwait(false);
result.GetAwaiter().GetResult();
}
My questions are:
- should async/await be used within
Task.Run()
(please explain, if possible)? - Is there a better way to wrap
_lib.Send(ISomething input)
under the above mentioned conditions? // the signature is:public Task Send(ISomething input);