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:
- Upgrade to AutoMapper v13+ where the DI functionality is included in the main package
- Remove the deprecated
AutoMapper.Extensions.Microsoft.DependencyInjection
package
Step-by-Step Fix
- Modify your project file (
.csproj
):
<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.
- Update your service registration:
Your mapping profile remains unchanged:
public class AuthMapperProfile : Profile
{
public AuthMapperProfile()
{
CreateMap<ApplicationUser, UserDto>().ReverseMap();
// Other mappings
}
}
- Modify your service extension:
public static class AutoMapperServiceExtension
{
public static void ConfigureAutoMappers(this IServiceCollection services)
{
services.AddAutoMapper(typeof(AuthMapperProfile));
}
}
- Register in Program.cs (unchanged):
// Configure Services
builder.Services.ConfigureAutoMappers();
Why This Works
- AutoMapper v13+ includes the DI registration functionality directly in the main package
- Removing the separate DI package eliminates conflicting method definitions
- 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
// In Program.cs
builder.Services.AddAutoMapper(typeof(Startup).Assembly);
Multiple Specific Profiles
builder.Services.AddAutoMapper(
typeof(UserProfile),
typeof(ProductProfile),
typeof(OrderProfile));
Important Considerations
Namespace Conflicts: After removing the DI package, ensure you're using the correct namespace:
csharpusing AutoMapper; // Correct namespace for v13+
Compatibility: Verify .NET framework compatibility with your AutoMapper version:
AutoMapper Version | .NET Core Compatibility |
---|---|
v12.x | .NET Core 3.1+ |
v13.x | .NET 6+ |
- Project Cleanup: After package changes:
- Clean solution (
dotnet clean
) - Delete
bin
andobj
folders - Restart Visual Studio/Rider
- Clean solution (
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.