2

I have this code in an ASP.NET Core 8 Web API using Fluent Validation:

public class CreateBranchBulkUploadCommand : IRequest<Response<string>>
{
    public IFormFile File { get; set; }
}

public class CreateBranchBulkUploadCommandHandler : IRequestHandler<CreateBranchBulkUploadCommand, Response<string>>
{
    private readonly IBranchRepositoryAsync _branchRepository;
    private readonly ICompanyRepositoryAsync _companyRepository;
    private readonly ILogger<CreateBranchBulkUploadCommandHandler> _logger;

    public CreateBranchBulkUploadCommandHandler(
        IBranchRepositoryAsync branchRepository,
        ICompanyRepositoryAsync companyRepository,
        ILogger<CreateBranchBulkUploadCommandHandler> logger)
    {
        _branchRepository = branchRepository;
        _companyRepository = companyRepository;
        _logger = logger;
    }

    public async Task<Response<string>> Handle(CreateBranchBulkUploadCommand request, CancellationToken cancellationToken)
    {
        try
        {
            if (request.File == null || request.File.Length == 0)
                return Response<string>.Failure("Uploaded file is empty.");

            if (!Path.GetExtension(request.File.FileName).Equals(".xlsx", StringComparison.OrdinalIgnoreCase))
                return Response<string>.Failure("Invalid file format. Only .xlsx files are allowed.");

            using (var stream = new MemoryStream())
            {
                await request.File.CopyToAsync(stream, cancellationToken);
                using (var package = new ExcelPackage(stream))
                {
                    var worksheet = package.Workbook.Worksheets.FirstOrDefault();
                    if (worksheet == null)
                        return Response<string>.Failure("The uploaded file does not contain any worksheet.");

                    var branches = new List<CreateBranchCommand>();

                    for (int row = 2; row <= worksheet.Dimension.Rows; row++)
                    {
                        var companyIdString = worksheet.Cells[row, 1].Text;
                        Guid? companyId = null;
                        if (Guid.TryParse(companyIdString, out Guid parsedCompanyId))
                            companyId = parsedCompanyId;

                        var branchName = worksheet.Cells[row, 2].Text;
                        var branchType = worksheet.Cells[row, 3].Text;
                        var branchAddress = worksheet.Cells[row, 4].Text;
                        var description = worksheet.Cells[row, 5].Text;
                        var telephone1 = worksheet.Cells[row, 6].Text;
                        var telephone2 = worksheet.Cells[row, 7].Text;
                        var email = worksheet.Cells[row, 8].Text;

                        var branchCommand = new CreateBranchCommand
                        {
                            Id = Guid.NewGuid(),
                            CompanyId = companyId,
                            BranchName = branchName,
                            BranchType = branchType,
                            BranchAddress = branchAddress,
                            Description = description,
                            Telephone1 = telephone1,
                            Telephone2 = telephone2,
                            Email = email
                        };

                        branches.Add(branchCommand);
                    }

                    foreach (var branch in branches)
                    {
                        var validationResult = await ValidateBranchAsync(branch, cancellationToken);

                        if (!validationResult.IsValid)
                        {
                            var errorMessages = string.Join(", ", validationResult.Errors.Select(e => e.ErrorMessage));
                            return Response<string>.Failure($"Validation failed for branch with name '{branch.BranchName}': {errorMessages}");
                        }

                        var handler = new CreateBranchCommandHandler(_companyRepository, _branchRepository, _logger);
                        await handler.Handle(branch, cancellationToken);
                    }
                }
            }

            return Response<string>.Success("Branches imported successfully.");
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error importing branches from Excel.");
            return Response<string>.Failure("An error occurred while importing branches.");
        }
    }

    private async Task<ValidationResult> ValidateBranchAsync(CreateBranchCommand branch, CancellationToken cancellationToken)
    {
        var validator = new CreateBranchCommandValidator(_companyRepository, _branchRepository);
        return await validator.ValidateAsync(branch, cancellationToken);
    }
}

I get this error:

Error CS1503
Argument 3: cannot convert from 'Microsoft.Extensions.Logging.ILogger<Organizations.Branches.Command.UploadBranch.CreateBranchBulkUploadCommandHandler>' to 'Microsoft.Extensions.Logging.ILogger<Organizations.Branches.Command.CreateBranch.CreateBranchCommandHandler>'

It highlights:

_logger

on this line:

var handler = new CreateBranchCommandHandler(_companyRepository, _branchRepository, _logger);

1 Answer 1

3

Create another variable with ILogger<CreateBranchCommandHandler> and provide it to CreateBranchCommandHandler parameter.

private readonly ILogger<CreateBranchCommandHandler> _createBranchCommandLogger;

public CreateBranchBulkUploadCommandHandler(
    IBranchRepositoryAsync branchRepository,
    ICompanyRepositoryAsync companyRepository,
    ILogger<CreateBranchBulkUploadCommandHandler> logger,
    ILogger<CreateBranchCommandHandler> createBranchCommandLogger)
{
    ...
    _createBranchCommandLogger = createBranchCommandLogger;
}
var handler = new CreateBranchCommandHandler(_companyRepository, _branchRepository, _createBranchCommandLogger);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.