首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用类库时注册AutoMapper配置文件?

如何在使用类库时注册AutoMapper配置文件?
EN

Stack Overflow用户
提问于 2019-12-06 03:09:21
回答 2查看 2.6K关注 0票数 0

所以,我有一个具有以下结构的项目,

-我的-项目

.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只能从上面获得一个注册。如何注册上述层的所有映射配置文件?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-12-06 03:23:13

您需要获取当前域中的所有程序集。Assembly.GetExecutingAssembly()只返回主程序集。若要扫描域中的所有程序集,请使用AppDomain.CurrentDomain.GetAssemblies()。你应该可以把它传递给IServiceCollection.AddAutoMapper(...)

services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

票数 2
EN

Stack Overflow用户

发布于 2019-12-13 01:06:50

看来,AddAutoMapper()方法只能使用一次。因此,我必须手动将配置文件添加到MapperConfiguration中。然后使用该配置在IServiceCollection中注册映射器的实例(参见下面的示例),

代码语言:javascript
复制
MapperConfiguration mapperConfiguration = new MapperConfiguration(mapperConfig => {
   mapperConfig.AddProfile<ExampleMapProfile>();
});

services.AddSingleton(mapperConfiguration.CreateMapper());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59206290

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档