How inject service in AutoMapper profile class

前端 未结 3 2095
渐次进展
渐次进展 2020-12-29 05:00

I need to use a service layer in the AutoMapper profile class in ASP.NET Core but when I inject service in the constructor it does not work. For ex

3条回答
  •  死守一世寂寞
    2020-12-29 05:16

    I get it that this question is not recent but there is a nuget package for this: AutoMapperBuilder.

    You can get what you want by replacing this line:

    services.AddAutoMapper(typeof(UserProfile));
    

    with these:

    services.AddAutoMapperBuilder(builder =>
    {
         builder.Profiles.Add(new UserProfile(services.BuildServiceProvider().GetRequiredService()));
    });
    

提交回复
热议问题