尝试将 AutoMapper 添加到 .NetCore1.1 - 无法识别 services.AddAutoMapper() [英] Trying to add AutoMapper to .NetCore1.1 - not recognising services.AddAutoMapper()

查看:20
本文介绍了尝试将 AutoMapper 添加到 .NetCore1.1 - 无法识别 services.AddAutoMapper()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的项目中安装了以下 Nuget 包:

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

  • Automapper
  • AutoMapper.Extensions.Microsoft.DependencyInjection

我在 Startup.csConfigureServices 中添加了一行.

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

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

我仍然在 services.AddAutoMapper() 下看到一条红线.它说:

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

Call 在以下方法或属性之间不明确:ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Assembly[]) 和 ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Type[])

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

为什么会这样?我读过的所有 .NET Core 添加自动映射器指南都表明这样做的方式.

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

推荐答案

我遇到了同样的问题,所以检查了 源代码和测试 以获得指导.似乎您需要在要扫描的程序集中传递程序集或标记类型".我进行了以下操作,因为我的 Profile 类与 Startup 类在同一个程序集中.

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

这篇关于尝试将 AutoMapper 添加到 .NetCore1.1 - 无法识别 services.AddAutoMapper()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆