c# - 使用类库时如何注册 AutoMapper 配置文件?
问题描述
所以,我有一个具有以下结构的项目,
-- My-Project
-- Infrastructure-Layer
---- InfraMappingProfiles.cs(AutoMapper Mapping Profiles)
---- DI.cs(这是一个调用的扩展方法services.AddAutoMapper(Assembly.GetExecutingAssembly());
)
--- Application-Layer
---- ApplicationMappingProfiles。 cs (AutoMapper Mapping Profiles)
---- DI.cs (这是一个调用的扩展方法services.AddAutoMapper(Assembly.GetExecutingAssembly());
)
--- Common-Layer
---- CommonMappingProfiles.cs (AutoMapper Mapping Profiles)
---- DI.cs (这是调用的扩展方法services.AddAutoMapper(Assembly.GetExecutingAssembly());
)
--- Api-Layer
---- ApiMappingProfiles.cs (AutoMapper Mapping Profiles)
---- DI.cs (这是调用的扩展方法services.AddLayers() // register IServiceCollection from all layers
)
所以,出于某种原因,AutoMapper 只从上面提取一个注册。如何注册上述层的所有映射配置文件?
解决方案
您需要获取当前域中的所有程序集。Assembly.GetExecutingAssembly()
只会返回主要组件。要扫描域中的所有程序集,您将使用AppDomain.CurrentDomain.GetAssemblies()
. 您应该能够将其传递给IServiceCollection.AddAutoMapper(...)
:
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
推荐阅读
- node.js - 打字稿:任何人都有一种优雅的方式来接收具有已知时区的日期字符串并将其转换为 UTC
- wordpress - WordPress.org 添加低级代码/添加外部 API?
- c++ - C++:constexpr 对变量暗示隐式 const 不适用于引用
- python - 关系字段Many2one和One2many中的AttributeError
- google-apps-script - 在谷歌应用脚本中的特定行之间添加行
- django - Django,模板。无法在 if 语句中传递变量
- javascript - 我在哪里为我的网站上传套接字 io 服务器 javascript 文件?
- ssh - 将 SSH 端口 22 更改为 800
- python - Series.str.split 返回 NaN 和意外的数据类型
- wordpress - 更改某个 WooCommerce 产品(ID)的“商店基地”标签