所以,我有一个具有以下结构的项目,
-我的项目-基础设施层---- InfraMappingProfiles.cs(AutoMapper映射配置文件)---- DI.cs(这是调用services.AddAutoMapper(Assembly.GetExecutingAssembly());
的扩展方法)---应用层---- ApplicationMappingProfiles.cs(AutoMapper映射配置文件)---- DI.cs(这是调用services.AddAutoMapper(Assembly.GetExecutingAssembly());
的扩展方法)---普通层---- CommonMappingProfiles.cs(AutoMapper映射配置文件)---- DI.cs(这是调用services.AddAutoMapper(Assembly.GetExecutingAssembly());
的扩展方法)--- Api层---- ApiMappingProfiles.cs(AutoMapper映射配置文件)---- DI.cs(这是调用services.AddLayers() // register IServiceCollection from all layers
的扩展方法)
因此,出于某种原因,AutoMapper仅从上述选项中获取一个注册。如何注册上述层中的所有映射配置文件?
您需要获取当前域中的所有程序集。 Assembly.GetExecutingAssembly()
仅将返回主装配。要扫描域中的所有程序集,请使用AppDomain.CurrentDomain.GetAssemblies()
。您应该可以将其传递给IServiceCollection.AddAutoMapper(...)
:
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());