Consider Calling ConfigureAwait on the awaited task

383 views Asked by At

I'm developing an web API using CQRS pattern. I have a command to Create Author.

Here is my Command Handler:

internal sealed class AddCommandHandler : ICommandHandler<CreateAuthorCommand, Author>
{
    private readonly IUnitOfWork _unitOfWork;

    public AddCommandHandler(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
    }

    public async Task<Result<Author>> Handle(CreateAuthorCommand command)
    {
        var nameResult = Name.Create(command.FirstName, command.LastName);
        var birthDateResult = BirthDate.Create(command.DateOfBirth);
        var mainCategoryResult = Entities.Authors.MainCategory.Create(command.MainCategory);

        var authorResult = Result.Combine(nameResult, birthDateResult, mainCategoryResult)
                            .Map(() => new Author(nameResult.Value, birthDateResult.Value, null, mainCategoryResult.Value));

        if (authorResult.IsFailure)
            return Result.Failure<Author>(authorResult.Error);

        await _unitOfWork.AuthorRepository.AddAsync(authorResult.Value);

        await _unitOfWork.SaveChangesAsync();

        return Result.Success(authorResult.Value);
    }
}

I have Microsoft.CodeAnalysis.FxCopAnalyzers and Roslynator.Analyzers nuget packages installed in my project. These packages shows the following Warning and Message for await _unitOfWork.AuthorRepository.AddAsync(authorResult.Value) and await _unitOfWork.SaveChangesAsync().

CA2007 Consider Calling ConfigureAwait on the awaited task

RCS1090 Call 'ConfigureAwait(false).'

There are lots of questions posted on this .ConfigureAwait

But can anyone explain me in layman terms on why I should do this? Adding .ConfigureAwait(false) removed the warning and message. But I need to understand this clearly on when to .ConfigureAwait(true) and when to .ConfigureAwait(false). Please assist and teach me.

0

There are 0 answers