I have the newest MediatR (12.2.0) and I have a synchronous method which returns nothing. Do I really need to use "async Task" instead of "Task"? Because the following method gives me "not all code paths return a value":
public class UploadFileCommandHandler(IFileUploadingService fileUploadingService)
: IRequestHandler<UploadFileCommand>
{
private readonly IFileUploadingService _fileUploadingService = fileUploadingService;
public /* async */ Task Handle(UploadFileCommand request, CancellationToken cancellationToken)
{
_fileUploadingService.UploadFile(request.File); // <- this method returns VOID, not a task, see it below (the interface IFileUploadingService)
}
}
public interface IFileUploadingService
{
void UploadFile(File file);
}
I have to add "async" bur then I have the warning "async method lack of await". What is the correct solution?
>Solution :
Based on the name ideally you should refactor IFileUploadingService.UploadFile to be async ("truly" async), since it seems to be an IO-bound operation.
If the interface/implementation is outside of your control then usual approach would be to return Task.CompletedTask:
public class UploadFileCommandHandler(IFileUploadingService fileUploadingService)
: IRequestHandler<UploadFileCommand>
{
private readonly IFileUploadingService _fileUploadingService = fileUploadingService;
public Task Handle(UploadFileCommand request, CancellationToken cancellationToken)
{
_fileUploadingService.UploadFile(request.File); // <- this method returns VOID, not a task, see it below (the interface IFileUploadingService)
return Task.CompletedTask;
}
}