Files
PowderCoatingLogix/AUTOMAPPER_TROUBLESHOOTING.md
2026-04-23 21:38:24 -04:00

5.8 KiB

AutoMapper Configuration - Alternative Approach Applied

🔧 Updated Configuration Method

If you're still seeing the MapperConfiguration constructor error, I've applied an alternative configuration approach that's more compatible.

New Configuration (Both Web & API)

Previous Approach:

var mapperConfig = new MapperConfiguration(cfg =>
{
    cfg.AddProfile(new CustomerProfile());
    cfg.AddProfile(new JobProfile());
});
IMapper mapper = mapperConfig.CreateMapper();
builder.Services.AddSingleton(mapper);
builder.Services.AddSingleton<IMapper>(mapper);

New Approach (Now Applied):

builder.Services.AddSingleton<IMapper>(sp =>
{
    var config = new MapperConfiguration(cfg =>
    {
        cfg.AddProfile(new CustomerProfile());
        cfg.AddProfile(new JobProfile());
    });
    return config.CreateMapper();
});

💡 Why This Works Better

  1. Service Provider Factory - Configuration happens inside DI factory
  2. Cleaner Scope - No intermediate variables in Program.cs
  3. Lazy Loading - Mapper only created when first requested
  4. Single Registration - Only registers IMapper interface once

🔍 Troubleshooting Steps

If you're still getting the constructor error, try these steps:

Step 1: Clean Everything

# Clean all build artifacts
dotnet clean

# Clear NuGet cache
dotnet nuget locals all --clear

Step 2: Verify Package Versions

# Check installed packages
dotnet list package

# Should show:
# AutoMapper 16.0.0 in Application, Web, and API projects

Step 3: Restore Packages

# Force restore
dotnet restore --force

Step 4: Build

# Build solution
dotnet build

📦 Required Package Versions

Make sure these are in your project files:

PowderCoating.Application.csproj

<PackageReference Include="AutoMapper" Version="16.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.0" />

PowderCoating.Web.csproj

<PackageReference Include="AutoMapper" Version="16.0.0" />

PowderCoating.Api.csproj

<PackageReference Include="AutoMapper" Version="16.0.0" />

🐛 Common Issues & Solutions

Issue 1: "MapperConfiguration does not contain a constructor..."

Cause: NuGet cache has old AutoMapper version
Solution:

dotnet nuget locals all --clear
dotnet restore --force
dotnet build

Issue 2: "Profile not found"

Cause: Missing using statement
Solution: Ensure this is in Program.cs:

using AutoMapper;
using PowderCoating.Application.Mappings;

Issue 3: "Cannot resolve IMapper"

Cause: Not registered in DI
Solution: Check the configuration is inside Program.cs before var app = builder.Build();

Issue 4: "Package downgrade warning"

Cause: Logging.Abstractions version mismatch
Solution: Update Application project:

<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.0" />

🔄 Alternative: Use AutoMapper.Extensions (If All Else Fails)

If you absolutely cannot get the manual configuration working, you can revert to using the Extensions package:

Add Package:

<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="16.0.0" />

Configure:

builder.Services.AddAutoMapper(typeof(CustomerProfile).Assembly);

However, the manual configuration should work and is preferred for the reasons stated above.

Verification Test

After building successfully, create a simple test controller:

[ApiController]
[Route("api/test")]
public class TestController : ControllerBase
{
    private readonly IMapper _mapper;

    public TestController(IMapper mapper)
    {
        _mapper = mapper;
    }

    [HttpGet]
    public IActionResult Test()
    {
        var customer = new Customer 
        { 
            Id = 1, 
            CompanyName = "Test Company",
            Email = "test@test.com"
        };
        
        var dto = _mapper.Map<CustomerDto>(customer);
        
        return Ok(new { 
            success = true, 
            mapped = dto 
        });
    }
}

Run the API and navigate to: https://localhost:7002/api/test

If it returns the mapped DTO, AutoMapper is working correctly!

📋 Final Checklist

  • Cleared NuGet cache
  • Deleted bin/ and obj/ folders
  • Restored packages with --force
  • AutoMapper 16.0.0 in all projects
  • Microsoft.Extensions.Logging.Abstractions 10.0.0 in Application
  • Using statements present in Program.cs
  • Configuration inside service registration (before builder.Build())
  • Both Profile classes exist in Application/Mappings/
  • Build succeeds without errors

🎯 Expected Build Output

dotnet build

# Should output:
Microsoft (R) Build Engine version 17.8.0+...
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  PowderCoating.Shared -> bin\Debug\net8.0\PowderCoating.Shared.dll
  PowderCoating.Core -> bin\Debug\net8.0\PowderCoating.Core.dll
  PowderCoating.Application -> bin\Debug\net8.0\PowderCoating.Application.dll
  PowderCoating.Infrastructure -> bin\Debug\net8.0\PowderCoating.Infrastructure.dll
  PowderCoating.Web -> bin\Debug\net8.0\PowderCoating.Web.dll
  PowderCoating.Api -> bin\Debug\net8.0\PowderCoating.Api.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:XX.XX

💡 If Still Having Issues

  1. Share the exact error message - The full error with line numbers
  2. Check your .csproj files - Ensure package versions match
  3. Verify Profile classes - Make sure they compile independently
  4. Try a new terminal/VS instance - Sometimes IDEs cache old assemblies

The new configuration method is now applied and should resolve the constructor error!