Skip to content

Resolving Ambiguous Call Error with AddAutoMapper in ASP.NET Core

Problem Statement

When working with ASP.NET Core 6 applications using AutoMapper, you may encounter a compilation error:

Error CS0121
The call is ambiguous between the following methods or properties: 
'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.AddAutoMapper(Microsoft.Extensions.DependencyInjection.IServiceCollection, params System.Type[])' 
and 
'Microsoft.Extensions.DependencyInjection.ServiceCollectionExtensions.AddAutoMapper(Microsoft.Extensions.DependencyInjection.IServiceCollection, params System.Type[])'

This error occurs when there are conflicting definitions of the AddAutoMapper extension method in your application. The conflict typically arises from referencing both the base AutoMapper package and the separate AutoMapper.Extensions.Microsoft.DependencyInjection package after upgrading to newer versions of AutoMapper.

Primary Solution: Upgrade and Remove Deprecated Package

The official solution documented in the AutoMapper v13 Upgrade Guide is:

  1. Upgrade to AutoMapper v13+ where the DI functionality is included in the main package
  2. Remove the deprecated AutoMapper.Extensions.Microsoft.DependencyInjection package

Step-by-Step Fix

  1. Modify your project file (.csproj):
xml
<ItemGroup>
    <!-- Other package references -->
    
    <!-- REMOVE this line -->
    <!-- <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" /> -->
    
    <!-- ADD or UPGRADE this package -->
    <PackageReference Include="AutoMapper" Version="13.0.0" />
</ItemGroup>

TIP

Replace 13.0.0 with the latest stable version of AutoMapper. Always verify compatibility with your .NET version.

  1. Update your service registration:

Your mapping profile remains unchanged:

csharp
public class AuthMapperProfile : Profile
{
    public AuthMapperProfile()
    {
        CreateMap<ApplicationUser, UserDto>().ReverseMap();
        // Other mappings
    }
}
  1. Modify your service extension:
csharp
public static class AutoMapperServiceExtension
{
    public static void ConfigureAutoMappers(this IServiceCollection services)
    {
        services.AddAutoMapper(typeof(AuthMapperProfile));
    }
}
  1. Register in Program.cs (unchanged):
csharp
// Configure Services
builder.Services.ConfigureAutoMappers();

Why This Works

  1. AutoMapper v13+ includes the DI registration functionality directly in the main package
  2. Removing the separate DI package eliminates conflicting method definitions
  3. The AddAutoMapper method is now sourced from a single assembly

Alternative Registration Approaches

For different scenarios, you can use these registration patterns:

Single Assembly Scan

csharp
// In Program.cs
builder.Services.AddAutoMapper(typeof(Startup).Assembly);

Multiple Specific Profiles

csharp
builder.Services.AddAutoMapper(
    typeof(UserProfile), 
    typeof(ProductProfile),
    typeof(OrderProfile));

Important Considerations

  1. Namespace Conflicts: After removing the DI package, ensure you're using the correct namespace:

    csharp
    using AutoMapper; // Correct namespace for v13+
  2. Compatibility: Verify .NET framework compatibility with your AutoMapper version:

AutoMapper Version.NET Core Compatibility
v12.x.NET Core 3.1+
v13.x.NET 6+
  1. Project Cleanup: After package changes:
    • Clean solution (dotnet clean)
    • Delete bin and obj folders
    • Restart Visual Studio/Rider

WARNING

Do not mix AutoMapper v13+ with AutoMapper.Extensions.Microsoft.DependencyInjection package. They are fundamentally incompatible and will reproduce the ambiguous method error.

Conclusion

The ambiguous method error with AddAutoMapper occurs when both the base AutoMapper package (v13+) and the legacy DI package are referenced. The permanent fix requires upgrading to AutoMapper v13 or later and removing the AutoMapper.Extensions.Microsoft.DependencyInjection package. This resolves the method conflict by consolidating the DI functionality into the main package.

Always consult the official AutoMapper Upgrade Guide for version-specific migration instructions when upgrading between major releases.