Search code examples
c#asp.net-coreautomapper

ASP.NET Core AutoMapper: How to resolve Error CS0121 The call is ambiguous between the following methods or properties


I developed an ASP.NET Core 6 Web API about a year ago using AutoMapper. It was working then, but suddenly I got this 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[])'

Also I found that:

AutoMapper.Extensions.Microsoft.DependencyInjection Version-12.0.1

that is used is deprecated, because there is no higher version so I downgrade to:

AutoMapper.Extensions.Microsoft.DependencyInjection Version-12.0.0

Then:

.AddAutoMapper

was highlighted in:

services.AddAutoMapper(typeof(AuthMapperProfile));

Kindly help resolve it.

Project file:

<Project Sdk="Microsoft.NET.Sdk.Web">
    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
        <Nullable>disable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
        <PackageReference Include="FluentValidation.AspNetCore" Version="11.1.2" />
        <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.4" />
        <PackageReference Include="Microsoft.AspNetCore.Http"  Version="2.2.2" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.4" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.0.0" />
        <PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="2.1.1" />
        <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.4" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.4">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
        <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
        <PackageReference Include="NETCore.MailKit" Version="2.0.3" />
        <PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
        <PackageReference Include="Serilog.Sinks.Console" Version="4.0.1" />
        <PackageReference Include="Serilog.Sinks.File" Version="5.0.0" />
        <PackageReference Include="Serilog.Sinks.RavenDB" Version="3.0.0" />
        <PackageReference Include="ServiceStack.Interfaces" Version="6.0.2" />
        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
        <PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
        <PackageReference Include="System.ServiceModel.Duplex" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.Federation" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.Http" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.NetTcp" Version="4.8.*" />
        <PackageReference Include="System.ServiceModel.Security" Version="4.8.*" />
    </ItemGroup>

AuthMapperProfile:

public class AuthMapperProfile : Profile
{
    public AuthMapperProfile()
    {
        CreateMap<ApplicationUser, UserDto>().ReverseMap();
        CreateMap<ApplicationUser, ForgotPasswordDto>().ReverseMap();
        CreateMap<ApplicationUser, ResetPasswordDto>().ReverseMap();
        CreateMap<ApplicationUser, UpdatePasswordDto>().ReverseMap();
        CreateMap<ApplicationUser, MustChangePasswordDto>().ReverseMap();
        CreateMap<ApplicationUser, PasswordChangeDto>().ReverseMap();
    }
}

AutoMapperServiceExtension:

public static class AutoMapperServiceExtension
{
    public static void ConfigureAutoMappers(this IServiceCollection services)
    {
        services.AddAutoMapper(typeof(AuthMapperProfile));
    }
}

Program.cs:

// Configure AutoMapper
builder.Services.ConfigureAutoMappers();

Solution

  • See this web page with the v13 upgrade instructions.

    Most importantly:

    AddAutoMapper is part of the core package and the DI package is discontinued

    Therefore:

    • When you upgrade your AutoMapper to v13, it now includes the .AddAutoMapper() extension method in the base Nuget package
    • Remove the reference to the AutoMapper.Extensions.Microsoft.DependencyInjection package - no longer needed