gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:32:52 27 4
gpt4 key购买 nike

我在我的 ASP.NET Core 2.1 Web 应用程序中使用 AutoMapper 7.0.1 和 AutoMapper.Extensions.Microsoft.DependencyInjection 5.0.1。当我映射到未配置 ConstructUsingServiceLocator() 的类型时,映射有效。当我映射到配置有 ConstructUsingServiceLocator() 的类型时,它会抛出以下内容:

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

我正在遵循此处给出的将 AutoMapper 与 ASP.NET Core 结合使用的最新指南:How to pass a service from .net core di container to a new object created with automapper

我在一个全新的项目中用一个最小的例子重现了这个。以下是相关部分:

新建项目 > APS.NET Core Web 应用程序 > Web 应用程序

安装 AutoMapper 7.0.1 和 AutoMapper.Extensions.Microsoft.DependencyInjection 5.0.1 Nuget 包。

来源:

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

目的地:

public class Destination
{
private readonly IDestinationRepository _repo;

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

public string Name { get; set; }
}

IDestinationRepository:

public interface IDestinationRepository
{
}

目标存储库:

public class DestinationRepository : IDestinationRepository
{
}

映射配置文件:

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

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();
}

索引模型:

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!!
}
}

以上在 _mapper.Map<Source, Destination>(source) 上失败调用上面列出的异常。我已经验证了 MappingProfile正在加载。

如果我更改 Destination ctor 是无参数的,它仍然失败。

但是,如果我删除 ConstructUsingServiceLocator()来自 MappingProfile (使用空的 Destination ctor),我的映射开始工作。

我在这里做错了什么?感谢您的帮助!

最佳答案

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

关于c# - 在 ASP.NET Core 2.1 中使用 ConstructUsingServiceLocator() 时出现 AutoMapperMappingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51965677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com