Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

the newest Mediatr and a handler with a synchronous method Handle: "not all code paths return a value"

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading