[英]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.