[英]AutoMapper not instantiating IMappingEngine using SimpleInjector and WebApi2
我开始在项目(WebApi2,框架4.5.1)中使用AutoMapper(来自Nuget的最新版本),并使用SimpleInjector(来自Nuget的最新版本)。
我的问题是我不知道如何配置SimpleInjector以便通过构造函数将IMappingEngine注入到我的模型中。
现在,我遇到了错误:未 映射的属性:MappingEngine
我正在使用IMappingEngine界面。
我有一个带有所有Mapper.CreateMap <>的AutoMapperProfile类
AutoMapperConfig的示例
public class WebApiAutomapperProfile : Profile
{
/// <summary>
/// The configure.
/// </summary>
protected override void Configure()
{
this.CreateMap<Entity, EntityModel>();
}
}
模型接收IMappingEngine的原因是某些映射属性内部具有其他映射。
在Global.asax (方法Application_Start())中,我正在调用:
GlobalConfiguration.Configure(WebApiConfig.Register);
webApiContainer = new Container();
webApiContainer.Options.DefaultScopedLifestyle = new WebApiRequestLifestyle();
IocConfig.RegisterIoc(GlobalConfiguration.Configuration, webApiContainer);
IocConfig.cs
public static class IocConfig
{
public static void RegisterIoc(HttpConfiguration config, Container container)
{
InstallDependencies(container);
RegisterDependencyResolver(container);
}
private static void InstallDependencies(Container container)
{
new ServiceInstallerSimpleInjector().Install(container);
}
private static void RegisterDependencyResolver(Container container)
{
GlobalConfiguration.Configuration.DependencyResolver = new SimpleInjectorWebApiDependencyResolver(container);
}
ServiceInstallerSimpleInjector
public class ServiceInstallerSimpleInjector : IServiceInstallerSimpleInjector
{
// Automapper registrations
container.Register(typeof(ITypeMapFactory), typeof(TypeMapFactory), Lifestyle.Scoped);
container.RegisterCollection<IObjectMapper>(MapperRegistry.Mappers);
var configurationRegistration = Lifestyle.Scoped.CreateRegistration<ConfigurationStore>(container);
container.AddRegistration(typeof(IConfiguration), configurationRegistration);
container.AddRegistration(typeof(IConfigurationProvider), configurationRegistration);
// The initialization runs all the map creation once so it is then done when you come to do your mapping.
// You can create a map whenever you want, but this will slow your code down as the mapping creation involves reflection.
Mapper.Initialize(config =>
{
config.ConstructServicesUsing(container.GetInstance);
config.AddProfile(new WebApiAutomapperProfile());
config.AddGlobalIgnore("Errors");
config.AddGlobalIgnore("IsModelValid");
config.AddGlobalIgnore("BaseValidator");
config.AddGlobalIgnore("AuditInformation");
});
container.RegisterSingleton<IMappingEngine>(Mapper.Engine);
Mapper.AssertConfigurationIsValid();
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.Verify();
}
然后,每个Controller在构造函数中接收一个IMappingEngine并使用:
MappingEngine.Map<>
模型类样本
public class EntityModel : BaseModel.BaseModel<EntityModel >
{
public EntityModel(IMappingEngine mappingEngine) : base(mappingEngine)
{
}
}
基本模型
public abstract class BaseModel<T> : IBaseModel
where T : class
{
public IMappingEngine MappingEngine { get; set; }
protected BaseModel(IMappingEngine mappingEngine)
{
this.MappingEngine = mappingEngine;
}
}
错误消息显示:
Type needs to have a constructor with 0 args or only optional args\r\nParameter name: type
Mapping types:
Entity -> EntityModel
Model.Entity -> WebApi.Models.EntityModel
Destination path:
EntityModel
Source value:
System.Data.Entity.DynamicProxies.Entity_1D417730D5BE3DEAF6292D57AB49B32FA18136A1DCF74193E8716EC6EE4DC62B
问题是IMappingEngine mappingEngine没有注入到模型的构造函数中。 问题是如何使其工作。
当我尝试执行.Map时抛出错误
return this.MappingEngine.Map<Entity,EntityModel>(this.EntityRepository.AllMaterialized().FirstOrDefault());
这是Stacktrace
at WebApi.Controllers.Api.EntityController.Get() in c:\Users\Guillermo\Downloads\Backend\WebApi\Controllers\Api\EntityController.cs:line 108
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
有什么缺失或错误吗?
提前致谢! 吉列尔莫。
由于您的EntityController
已由Simple Injector正确解析,并且取决于IMapperEngine
,因此可以放心确保正确注入了映射器引擎。 可能发生的情况是当时注册的Mapper.Engine
没有正确初始化,但我只是在猜测。 Automapper专家应该能够看到这里出了什么问题。
但是,问题的核心是您尝试将依赖项注入到域实体中。 看一下Jimm Bogard (Automapper的创建者)的这篇文章,他解释了为什么这是一个坏主意。
一旦您在实体初始化期间停止要求服务依赖性,此问题将完全消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.