首页 > 解决方案 > 在构造函数中使用 DI 的 automapper Ivalueresolver

问题描述

基本上我得到了相同的:Asp.net core 2.0 AutoMapper IValueResolver 依赖注入

但没有答案。

所以我得到了:

public static void AddMapperConfiguration(this IServiceCollection services)
{
    var spb = services.BuildServiceProvider();

    var mapperConfiguration = new MapperConfiguration(cfg =>
    {
        cfg.AddProfile<CmsDataMappingProfile>();
        cfg.AddProfile<ClsMappingProfile>();
        cfg.AddProfile<ClsMockDataMappingProfile>();
        cfg.AddProfile<LpisMappingProfile>();

        cfg.AllowNullCollections = true;
        cfg.AllowNullDestinationValues = true;
        cfg.ConstructServicesUsing(spb.GetService); //read it the other post
    });

    mapperConfiguration.AssertConfigurationIsValid();
    services.AddSingleton<IMapper>(sp => new Mapper(mapperConfiguration)); //just testing
}

并作为解析器:

public class CurrencyResolver : IValueResolver<List<VehicleAndQuote>, LeasingPlan, List<Duration>>
{
    private readonly ICountryProfileConnector _countryProfileConnector;

    public CurrencyResolver(ICountryProfileConnector countryProfileConnector)
    {
        _countryProfileConnector = countryProfileConnector;
    }

     ....left out for readability
}

和想法?我在这个问题上遇到了无参数构造函数错误。

谢谢!

标签: c#.net-coreautomapper

解决方案


推荐阅读