简体   繁体   English

在ASP.NET Core 2.1中使用ConstructUsingServiceLocator()时出现AutoMapperMappingException

[英]AutoMapperMappingException when using ConstructUsingServiceLocator() in ASP.NET Core 2.1

I'm using AutoMapper 7.0.1 and AutoMapper.Extensions.Microsoft.DependencyInjection 5.0.1 in my ASP.NET Core 2.1 web application. 我在ASP.NET Core 2.1 Web应用程序中使用了AutoMapper 7.0.1和AutoMapper.Extensions.Microsoft.DependencyInjection 5.0.1。 When I map to a type that isn't configured with ConstructUsingServiceLocator() , the mapping works. 当我映射到未使用ConstructUsingServiceLocator()配置的类型时,映射将起作用。 When I map to a type that is configured with ConstructUsingServiceLocator() , it throws the following: 当我映射到使用ConstructUsingServiceLocator()配置的类型时,它将引发以下情况:

AutoMapperMappingException: Cannot create an instance of type 
AutoMapperTest.Destination
AutoMapper.MappingOperationOptions<TSource, TDestination>.CreateInstance<T>() in MappingOperationOptions.cs, line 47

I'm following the latest guidance for using AutoMapper with ASP.NET Core given here: How to pass a service from .net core di container to a new object created with automapper 我遵循此处给出的有关将AutoMapper与ASP.NET Core一起使用的最新指南: 如何将服务从.net core di容器传递到使用automapper创建的新对象

I've reproduced this with a minimal example in a brand new project. 我已经在一个全新项目中以最少的示例复制了此内容。 Here's the relevant parts: 以下是相关部分:

New project > APS.NET Core Web Application > Web Application 新项目> APS.NET Core Web应用程序> Web应用程序

Install AutoMapper 7.0.1 and AutoMapper.Extensions.Microsoft.DependencyInjection 5.0.1 Nuget packages. 安装AutoMapper 7.0.1和AutoMapper.Extensions.Microsoft.DependencyInjection 5.0.1 Nuget程序包。

Source: 资源:

public class Source
{
    public string Name { get; set; }
}

Destination: 目的地:

public class Destination
{
    private readonly IDestinationRepository _repo;

    public Destination(IDestinationRepository repo)
    {
        _repo = repo ?? throw new ArgumentNullException(nameof(repo));
    }

    public string Name { get; set; }
}

IDestinationRepository: IDestinationRepository:

public interface IDestinationRepository
{
}

DestinationRepository: DestinationRepository:

public class DestinationRepository : IDestinationRepository
{
}

MappingProfile: MappingProfile:

public class MappingProfile : Profile
{
    public MappingProfile()
    {           
        CreateMap<Source, Destination>().ConstructUsingServiceLocator();
    }
}

Startup.ConfigureServices(IServiceCollection services): Startup.ConfigureServices(IServiceCollection服务):

public void ConfigureServices(IServiceCollection services)
{
    services.AddScoped<IDestinationRepository, DestinationRepository>();

    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

    services.AddAutoMapper();
}

IndexModel: IndexModel:

public class IndexModel : PageModel
{
    private readonly IMapper _mapper;

    public IndexModel(IMapper mapper)
    {
        _mapper = mapper;
    }

    public void OnGet()
    {
        _mapper.ConfigurationProvider.AssertConfigurationIsValid();            // <- Succeeds
        var repo = _mapper.ServiceCtor.Invoke(typeof(IDestinationRepository)); // <- repo is non-null

        var source = new Source {Name = "Test"};
        var destination = _mapper.Map<Source, Destination>(source);            // <- Fails!!
    }
}

The above fails on the _mapper.Map<Source, Destination>(source) call with the exception listed above. 上面的例外情况在_mapper.Map<Source, Destination>(source)调用上失败。 I've verified MappingProfile is getting loaded. 我已经验证了MappingProfile正在加载。

If I change the Destination ctor to be parameterless, it still fails. 如果将Destination ctor更改为无参数,它仍然会失败。

However, if I remove ConstructUsingServiceLocator() from MappingProfile (with the empty Destination ctor), my mapping starts to work. 但是,如果我从MappingProfile (带有空的Destination ctor)中删除了ConstructUsingServiceLocator() ),则我的映射开始工作。

What am I doing wrong here? 我在这里做错了什么? Thanks for any help! 谢谢你的帮助!

您的Destination类未在di容器中注册,因此无法让di容器创建Destination类的新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 检索ASP.NET Core 2.1 Cookie项时为null - ASP.NET Core 2.1 Cookie Item null when retrieved 当我们使用 ASP.NET API Core2.1 时如何在 MongoDB 中使用多集合进行操作 - How to manipulate with multi collection in MongoDB when we using ASP.NET API Core2.1 ASP.NET Core 2.1 在函数外使用变量 - ASP.NET Core 2.1 Using variable outside a function 在asp.net core 2.1上使用Hangfire发送电子邮件 - Sending emails using Hangfire on asp.net core 2.1 使用 ADFS 授权对 ASP.NET Core 2.1 应用程序的请求 - Authorize requests to ASP.NET Core 2.1 application using ADFS 在 localhost 上使用 asp.net core 2.1 身份时,TempData 丢失 - TempData lost when using asp.net core 2.1 identity on localhost ASP.NET Core 2.1中使用组合的不同用户类型 - Different User Types in ASP.NET Core 2.1 Using Composition ASP.NET 核心 - AutoMapper.AutoMapperMappingException:缺少类型 map 配置或不支持的映射 - ASP.NET Core - AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping Asp.net 核心 3.1 AutoMapperMappingException:缺少类型 map 配置或不支持的映射 - Asp.net core 3.1 AutoMapperMappingException: Missing type map configuration or unsupported mapping ASP.NET Core 2.1 XmlNodeReader - ASP.NET Core 2.1 XmlNodeReader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM