31

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.

Richard Garside
  • 87,839
  • 11
  • 80
  • 93
magna_nz
  • 1,243
  • 5
  • 23
  • 42

10 Answers10

62

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

Ikram Shah
  • 1,206
  • 1
  • 11
  • 12
27

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));
Robert Massa
  • 4,345
  • 1
  • 32
  • 44
  • What do you add if your profile classes are not in the same assembly ? – Choco Oct 13 '17 at 01:22
  • 2
    `typeof(ATypeInTargetAssembly)` – Robert Massa Oct 15 '17 at 18:35
  • Yep, that's what I did in the end and it worked... I found you can also add a list of these, thus allowing for multiple dependencies being configured. Might be helpful for others reading these comments to know this. – Choco Oct 16 '17 at 00:24
  • 4
    As Ikram Shah's answer points out... adding the nuget pkg `automapper.extensions.microsoft.dependencyinjection` is the answer to resolving extension method `AddAutoMapper()` when configuring services in `Startup.cs` like `services.AddAutoMapper();` – Adam Cox Apr 23 '18 at 19:57
  • You can use something like this: services.AddAutoMapper(typeof(AutoMappingProfile).GetTypeInfo().Assembly); – Avjol Sakaj Feb 13 '20 at 13:09
11

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.

magna_nz
  • 1,243
  • 5
  • 23
  • 42
6

Search this in nuget you can access automap

AutoMapper.Extensions.Microsoft.DependencyInjection
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
SinaNasiri
  • 83
  • 2
  • 3
3

Install package:

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

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

1

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.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
lbegazo
  • 51
  • 3
1

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.

Jatala
  • 93
  • 1
  • 11
1

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.

CristisS
  • 1,103
  • 1
  • 12
  • 31
0

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

-2

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