I am implementing a message dispatcher/handler where each handler must implement an interface like Task<Result> HandleAsync(IEvent myevent)
For example:
public async Task<Result> HandleAsync(CustomerChangedEvent ev)
{
await DoSomethingAsync();
...
return myResult;
}
But sometimes I need to run something which is sync only so no await and that as you know will give you a compiler warning. What I have done is to add await Task.CompletedTask;
in my code. Works fine AFAIK but is there any downside doing this?
Another alternative is to drop the async
but in that case I need to return a Task, right? For example to return Task.FromResult(myResult)
.
Is there any benefits for one method over another (or different approach), especially from a performance perspective (like is there any substantial cost to declare a method as async
. Personally, I prefer the first alternative. For some reason I find return Task.FromResult
harder to read and understand even if the first method adds a dummy command.
EDIT:
Sometimes it is maybe better to rethink the logic. "funatparties" commented why run sync code with an async method.
Reason is that I am implementing an outbox message handler where I am processing a queue with different messages to be processed and some have async operations other not. For each type of message I create a handler class which implements an interface.
I want adding handle a new message type I want it to be as few steps as possible. So basically what you do is to create a new handler which implements a specific interface and register the handler with my dispatcher class (which I probably can do automatically as well using Reflection).
The interface require you to implement Task. When reading the queue it will run the HandleAsync method using the registered handler (depending on message type).
A better approach maybe is that the handler can define either an sync or an async method depending on what I need to do.