我开始使用AutoMapper(Nuget的最新版本)到我的项目(WebApi2,框架4.5.1)和使用SimpleInjector(Nuget的最新版本)。AutoMapper没有实例化使用SimpleInjector和WebApi2的IMappingEngine
我的问题是,我不知道如何配置SimpleInjector以通过构造函数将IMappingEngine注入到模型中。
现在,我得到的错误: 未映射属性:MappingEngine
我使用的IMappingEngine接口。
我有一个AutoMapperProfile类与所有Mapper.CreateMap 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();
}
然后,每个控制器接收在构造一个IMappingEngine和用途:
MappingEngine.Map<>
模型类样品
public class EntityModel : BaseModel.BaseModel<EntityModel >
{
public EntityModel(IMappingEngine mappingEngine) : base(mappingEngine)
{
}
}
BaseModel
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没有被注入到模型的构造。问题是如何使它工作。
当我试图做一个错误时抛出错误。地图
return this.MappingEngine.Map<Entity,EntityModel>(this.EntityRepository.AllMaterialized().FirstOrDefault());
,这是堆栈跟踪
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)
遗漏或错误?
提前致谢!吉列尔莫。
嗨吉列尔莫。 Problaby你的问题只与Automapper和SimpleInjector绑定,WebApi就在旁边。您必须尝试以下方法以便轻松帮助您: 1.准备一个单元测试来解决问题。 2.尝试替换Automapper(例如使用Value Injecter https://valueinjecter.codeplex.com/以确定是否是映射问题)。 3.尝试替换SimpleInjector(例如用Windsor)。 这可能会在你的黑暗中提供一些亮光。 – Apocatastasis
你是否遇到任何异常?如果是这样,请将完整的异常与内部异常和堆栈跟踪一起发布。 – Steven
嗨@Apocatastasis,更改IoC容器不是我的选择,因为我正在为一个已经使用它的更大的“插件系统”做一个“插件系统”。 我知道这是一个映射问题。问题是IMappingEngine mappingEngine没有被注入到模型的构造函数中。问题是如何使它工作。 向上述问题添加了错误说明。 – polonskyg