首页 > 解决方案 > 使用类库时如何注册 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 只从上面提取一个注册。如何注册上述层的所有映射配置文件?

标签: c#.netautomapperasp.net-core-webapi

解决方案


您需要获取当前域中的所有程序集。Assembly.GetExecutingAssembly()只会返回主要组件。要扫描域中的所有程序集,您将使用AppDomain.CurrentDomain.GetAssemblies(). 您应该能够将其传递给IServiceCollection.AddAutoMapper(...)

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


推荐阅读