所以,我有一个具有以下结构的项目,
-我的-项目
.Layer=‘Layer 2’>-基础设施层
. Mapping =‘Mapping 2’>( InfraMappingProfiles.cs ) (AutoMapper测绘概况)
( DI.cs )(这是一种叫services.AddAutoMapper(Assembly.GetExecutingAssembly());的扩展方法)
.Layer=‘Layer 3’>应用层
. Mapping =‘Mapping 2’>( ApplicationMappingProfiles.cs ) (AutoMapper测绘概况)
( DI.cs )(这是一种叫services.AddAutoMapper(Assembly.GetExecutingAssembly());的扩展方法)
.-共同层
. Mapping =‘Mapping 2’>( CommonMappingProfiles.cs ) (AutoMapper测绘概况)
( DI.cs )(这是一种叫services.AddAutoMapper(Assembly.GetExecutingAssembly());的扩展方法)
.
. Mapping =‘Mapping 2’>( ApiMappingProfiles.cs ) (AutoMapper测绘概况)
( DI.cs )(这是一种叫services.AddLayers() // register IServiceCollection from all layers的扩展方法)
因此,出于某种原因,AutoMapper只能从上面获得一个注册。如何注册上述层的所有映射配置文件?
发布于 2019-12-06 03:23:13
您需要获取当前域中的所有程序集。Assembly.GetExecutingAssembly()只返回主程序集。若要扫描域中的所有程序集,请使用AppDomain.CurrentDomain.GetAssemblies()。你应该可以把它传递给IServiceCollection.AddAutoMapper(...)
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
发布于 2019-12-13 01:06:50
看来,AddAutoMapper()方法只能使用一次。因此,我必须手动将配置文件添加到MapperConfiguration中。然后使用该配置在IServiceCollection中注册映射器的实例(参见下面的示例),
MapperConfiguration mapperConfiguration = new MapperConfiguration(mapperConfig => {
mapperConfig.AddProfile<ExampleMapProfile>();
});
services.AddSingleton(mapperConfiguration.CreateMapper());https://stackoverflow.com/questions/59206290
复制相似问题