【问题标题】:Getting a CS0121 error in the ConfigureServices method in my Web API's Startup class在我的 Web API 的 Startup 类的 ConfigureServices 方法中出现 CS0121 错误
【发布时间】:2021-10-31 23:55:27
【问题描述】:

我一直在关注有关使用 .NET Core 2.2 构建 Web API 的教程。我正在使用.NET 5。我相信我得到的错误肯定有足够不同的变化,如果我使用.NET Core 2.2,我不会得到,但我不知道在哪里进行必要的改变(s)。我在 Stack Overflow 上进行了搜索,但没有找到任何可以解决我遇到的错误的方法。这是我的 Startup 类中的 ConfigureServices 方法中的代码:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<JobSearchContext>();
    services.AddScoped<IAuthorizedUserRepository, AuthorizedUserRepository>();
    services.AddAutoMapper(); //CS0121 error occurs here
    services.AddControllers();
}

我得到的错误文本是这样的:

CS0121 以下方法或属性之间的调用不明确:'ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Assembly[])' 和 'ServiceCollectionExtensions.AddAutoMapper(IServiceCollection, params Type[])'

.NET Core 2.2 和 .NET 5 之间发生了什么变化,以至于在我的 ConfigureServices 方法中对 AddAutoMapper 的调用会引发错误 CS0121?

【问题讨论】:

    标签: c#


    【解决方案1】:

    如果你使用 net 5 试试这个

    services.AddAutoMapper(typeof(Startup));
    

    但首先要创建一个继承自 AutoMapper 中的 Profile 类的类

    public class MappingProfile : Profile 
    {
       public MappingProfile()
       {
          CreateMap<class, classDto>();
       }
    }
    

    启动选项显示此配置文件将在同一个程序集中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 2022-12-11
      • 2020-09-14
      • 2011-12-14
      • 1970-01-01
      • 1970-01-01
      • 2016-01-08
      相关资源
      最近更新 更多