magna_nz
magna_nz

Reputation: 1273

Trying to add AutoMapper to .NetCore1.1 - not recognising services.AddAutoMapper()

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

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.

Upvotes: 31

Views: 18292

Answers (10)

Install package:

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

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

Upvotes: 3

user11475999
user11475999

Reputation:

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

Upvotes: 0

CristisS
CristisS

Reputation: 1163

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.

Upvotes: 1

Jatala
Jatala

Reputation: 103

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.

Upvotes: 1

SinaNasiri
SinaNasiri

Reputation: 83

Search this in nuget you can access automap

AutoMapper.Extensions.Microsoft.DependencyInjection

Upvotes: 6

Ikram Shah
Ikram Shah

Reputation: 1246

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

Upvotes: 63

Roman Raj Bajracharya
Roman Raj Bajracharya

Reputation: 47

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

Upvotes: -2

lbegazo
lbegazo

Reputation: 51

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.

Upvotes: 1

Robert Massa
Robert Massa

Reputation: 4608

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));

Upvotes: 27

magna_nz
magna_nz

Reputation: 1273

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.

Upvotes: 11

Related Questions