问题描述
我正在使用:
- AutoMapper 6.1.1
- AutoMapper.Extensions.Microsoft.DependencyInjection3.0.1
- AutoMapper 6.1.1
- AutoMapper.Extensions.Microsoft.DependencyInjection3.0.1
每次我调用mapper.map时,似乎都没有加载我的配置文件.我得到 AutoMapper.AutoMapperMappingException:'缺少类型映射配置或不支持的映射.'
It seems my profiles are not being loaded, every time I call the mapper.map I get AutoMapper.AutoMapperMappingException: 'Missing type map configuration or unsupported mapping.'
这是我的Startup.cs类ConfigureServices方法
Here my Startup.cs class ConfigureServices method
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//register automapper
services.AddAutoMapper();
.
.
}
在另一个名为xxxMappings的项目中,我有自己的映射配置文件.示例类
In another project called xxxMappings I have my mapping profiles.Example class
public class StatusMappingProfile : Profile
{
public StatusMappingProfile()
{
CreateMap<Status, StatusDTO>()
.ForMember(t => t.Id, s => s.MapFrom(d => d.Id))
.ForMember(t => t.Title, s => s.MapFrom(d => d.Name))
.ForMember(t => t.Color, s => s.MapFrom(d => d.Color));
}
public override string ProfileName
{
get { return this.GetType().Name; }
}
}
然后在服务类中以这种方式调用地图
And call the map this way in a service class
public StatusDTO GetById(int statusId)
{
var status = statusRepository.GetById(statusId);
return mapper.Map<Status, StatusDTO>(status); //map exception here
}
状态在调用statusRepository.GetById
status has values after calling statusRepository.GetById
对于我的Profile类,如果不是从Profile继承而是从MapperConfigurationExpression继承,则得到了如下所示的单元测试,表明映射是好的.
For my Profile classes, if instead of inherit from Profile I inherit from MapperConfigurationExpression I got a unit test like the one below saying the mapping is good.
[Fact]
public void TestStatusMapping()
{
var mappingProfile = new StatusMappingProfile();
var config = new MapperConfiguration(mappingProfile);
var mapper = new AutoMapper.Mapper(config);
(mapper as IMapper).ConfigurationProvider.AssertConfigurationIsValid();
}
我的猜测是我的映射未初始化.我该如何检查?我想念什么吗?我看到了AddAutoMapper()方法的重载
My guess is that my mappings are not being initialized.How can I check that? Am I missing something?I saw an overload for AddAutoMapper() method
services.AddAutoMapper(params Assembly[] assemblies)
我应该在xxxMappings项目中传递所有程序集.我该怎么办?
Should I pass all the assemblies in my xxxMappings project. How can I do that?
推荐答案
我知道了.由于我的映射位于不同的项目中,所以我做了两件事
I figure it out. Since my mappings are in a different project, I did two things
- 从我的API项目(位于Startup.cs所在的位置)中,添加对我的xxxMapprings项目的引用
-
在ConfigureServices中,我使用了重载AddAutoMapper来获取程序集:
- From my API project (where Startup.cs is located, added a reference to my xxxMapprings project)
in ConfigureServices I used the overload AddAutoMapper that gets an Assembly:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//register automapper
services.AddAutoMapper(Assembly.GetAssembly(typeof(StatusMappingProfile))); //If you have other mapping profiles defined, that profiles will be loaded too.
这篇关于Automapper配置文件未在启动中加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!