在Minimal API中使用AutoMapper报错:缺少类型映射配置如何解决?
我在连接Cosmos DB的新Minimal API项目中尝试使用AutoMapper,当获取所有对象时出现错误:“Missing type map configuration or unsupported mapping”,请问问题出在哪里?
相关代码
CarRequests类
public static class CarRequests { public static WebApplication CarEndpoints(this WebApplication app) { app.MapGet("/cars", CarRequests.GetAll); return app; } public static IResult GetAll(ICarService service, IMapper mapper) { var cars = service.GetAll(); var result = mapper.Map<List<CarDto>>(cars); return Results.Ok(result); } }
Car类
public class Car { [JsonProperty(PropertyName = "id")] public string Id { get; set; } [JsonProperty(PropertyName = "producent")] public Producent Producent { get; set; } [JsonProperty(PropertyName = "age")] public int Age { get; set; } [JsonProperty(PropertyName = "yearCreated")] public int YearCreated { get; set; } [JsonProperty(PropertyName = "engine")] public Engine Engine { get; set; } public Car() { Id= Guid.NewGuid().ToString(); YearCreated = DateTime.Now.Year - Age; } }
Engine类
public class Engine { [JsonProperty(PropertyName = "id")] public string Id { get; set; } [JsonProperty(PropertyName = "horsePower")] public int HorsePower { get; set; } [JsonProperty(PropertyName = "description")] public string Description { get; set; } public Engine() { Id= Guid.NewGuid().ToString(); } }
EngineDto类
public class EngineDto { [JsonProperty(PropertyName = "horsePower")] public int HorsePower { get; set; } [JsonProperty(PropertyName = "description")] public string Description { get; set; } }
CarDto类
public class CarDto { [JsonProperty(PropertyName = "producent")] public Producent Producent { get; set; } [JsonProperty(PropertyName = "age")] public int Age { get; set; } [JsonProperty(PropertyName = "yearCreated")] public EngineDto Engine { get; set; } }
服务类GetAll方法
public async Task<IEnumerable<Car>> GetAll() { string queryString = "select * from c"; var feedIterator = _container.GetItemQueryIterator<Car>(new QueryDefinition(queryString)); List<Car> cars = new List<Car>(); while (feedIterator.HasMoreResults) { var response = await feedIterator.ReadNextAsync(); cars.AddRange(response.ToList()); } return cars; }
AutoMapper配置类
public class AutoMapperProfile:Profile { public AutoMapperProfile() { CreateMap<Car, CarDto>(); CreateMap<Engine, EngineDto>(); CreateMap<CarDto, Car>(); CreateMap<EngineDto, Engine>(); } }
Program.cs中的AutoMapper注册
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
导致错误的核心原因有两个:
1. 异步方法未正确等待
你的GetAll服务方法返回的是Task<IEnumerable<Car>>,但在CarRequests.GetAll中直接调用service.GetAll()而没有加await,这意味着你传给AutoMapper的是一个Task对象,而不是实际的IEnumerable<Car>集合。AutoMapper没有配置从Task到List<CarDto>的映射,因此抛出错误。
修正方法:
将CarRequests.GetAll改为异步方法,添加await获取实际数据:
public static async Task<IResult> GetAll(ICarService service, IMapper mapper) { var cars = await service.GetAll(); var result = mapper.Map<List<CarDto>>(cars); return Results.Ok(result); }
2. CarDto属性的JsonProperty标注错误
在CarDto中,你将Engine属性的JsonProperty标记为"yearCreated",但对应的Car类中:
YearCreated是int类型字段,对应"yearCreated"Engine是Engine类型字段,对应"engine"
这种标注错误会导致AutoMapper无法正确匹配属性,进而引发映射配置缺失的错误。
修正方法:
修正CarDto的JsonProperty标注,同时如果需要返回YearCreated字段,添加对应的属性:
public class CarDto { [JsonProperty(PropertyName = "producent")] public Producent Producent { get; set; } [JsonProperty(PropertyName = "age")] public int Age { get; set; } [JsonProperty(PropertyName = "yearCreated")] public int YearCreated { get; set; } [JsonProperty(PropertyName = "engine")] public EngineDto Engine { get; set; } }
3. 额外验证(可选)
确认你的AutoMapperProfile所在的程序集被正确扫描到,你当前的注册方式AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies())已经覆盖了所有程序集,只要AutoMapperProfile在项目程序集中,就会被自动加载,这部分通常无需调整。
完成以上修正后,重新运行项目,映射错误应该会被解决。
内容的提问来源于stack exchange,提问作者Ptrk12

