29
votes

I've installed the following Nuget packages into my project:

  • Automapper
  • AutoMapper.Extensions.Microsoft.DependencyInjection

I have added the line to ConfigureServices in Startup.cs.

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();
    // . . .
    services.AddAutoMapper();
}

I'm still getting a red line under services.AddAutoMapper(). It says:

The Call is ambiguous between the following methods or properties: ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Assembly[]) and ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Type[])

Why is this happening? All the .NET Core add automapper guides I've read show to do it this way.

11
What version of the .NET Core SDK are you using?Shaun Luttin
dotnet --version says 1.0.0magna_nz
If I am not mistaken, you are using project.json. Is that right?Shaun Luttin
appsettings.jsonmagna_nz
I have the csprojmagna_nz

11 Answers

59
votes

I just ran into this problem and came to know that You need to include following Nuget package AutoMapper.Extensions.Microsoft.Dependencyinjection

26
votes

I'm running into the same issue, so checked the sourcecode and tests for guidance. It seems you need to pass either an assembly or a "marker type" inside the assembly you want scanned. I went for the following as my Profile classes are in the same assembly as the Startup class.

services.AddAutoMapper(typeof(Startup));
11
votes

Not sure why this is happening, it could very well be something to do with .NET Core 1.1 but I have found a workaround.

Instead of doing services.AddAutoMapper() in ConfigureServices, replace it with the below.

var config = new AutoMapper.MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new MappingProfile());
            });

            var mapper = config.CreateMapper();
            services.AddSingleton(mapper);

Where MappingProfile() is the class in which you have your CreateMap.

6
votes

Search this in nuget you can access automap

AutoMapper.Extensions.Microsoft.DependencyInjection
2
votes

Install package:

Install-Package AutoMapper.Extensions.Microsoft.DependencyInjection -Version 7.0.0

Nuget:
https://www.nuget.org/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/

1
votes

I had the same problem. To solved this issue I used the following versions of AutoMapper in project.csproj:

AutoMapper Version="6.0.1"
AutoMapper.Extensions.Microsoft.DependencyInjection Version="1.2.0"

Restore all packages and finally close and open the visual studio.

1
votes

I had the same problem (in .NET Core 2.2, Visual Studio Code 1.34.0, Ubuntu OS) despite having installed AutoMapper package (v8.1.0) and the AutoMapper.Extensions.Microsoft.Dependencyinjection package (v6.1.0).

The problem was solved by changing version of the packages in project.csproj as follows:

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

Somehow it looks like due to the package version mismatch.

1
votes

I had the same problem. In my case, the solution was a combination of the answers above:

  1. download the Nuget Package for Automapper -> Automapper by Jimmy Bogard
  2. download the Nuget Package for the Automapper Dependency Injection -> AutoMapper.Extensions.Microsoft.DependencyInjection by Jimmy Bogard
  3. add using Automapper; in the Startup.cs file

Now service.AddAutoMapper should work correctly. You can now initialize the Automapper to your liking.

0
votes

Add Nuget package for automapper.extensions.microsoft.dependencyinjection Latest Stable version 9

0
votes

If you are having issue with adding your auto mapper, It is better you check through the type and version you added. If it is not "AutoMapper.Extensions.Microsoft.DependencyInjection", then you won't be able to use "services.AddAutoMapper()". Sometimes, you might mistakenly add "AutoMapper

-2
votes

All you need to do is add using AutoMapper; at the top of Startup.cs file. Best of Luck!!!!