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

南笙酒味 提交于 2020-12-24 07:11:17

问题


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.


回答1:


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



回答2:


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




回答3:


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.




回答4:


Search this in nuget you can access automap

AutoMapper.Extensions.Microsoft.DependencyInjection



回答5:


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.




回答6:


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.




回答7:


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.




回答8:


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




回答9:


Install package:

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

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




回答10:


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



来源:https://stackoverflow.com/questions/42916182/trying-to-add-automapper-to-netcore1-1-not-recognising-services-addautomapper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!