AutoMapper.AutoMapperMappingException:缺少类型映射配置或不支持的映射。招摇消费另一个 API - .net 6

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping. Swagger Consuming another API - .net 6

我正在尝试在我的网络 api 应用程序中使用网络服务,但在测试时出现标题错误。我检查了我的映射,看起来没问题。我正在尝试遵循存储库模式。下面是我的代码:

型号:

 using System;
using System.ComponentModel.DataAnnotations;

namespace AppleApi.Models
{
    public class ApplicantInfo
    {
        [Key]
        public int Ctr { get; set; }

        public int Case_ID { get; set; }

        public int Actor_ID { get; set; }

        public string Name1 { get; set; }

        public string Name3 { get; set; }

        public string Full_Name { get; set; }

        public string Full_Name_Original { get; set; }

        public string Address { get; set; }

        public string Zip_Code { get; set; }

        public string State_ID { get; set; }

        public string Address { get; set; }

        public string Code { get; set; }

        public string State_ID { get; set; }

        public string Match { get; set; }

        public bool PartyChangedIndicator { get; set; }

    }
}

DTO上的代码

using System;
namespace AppleApi.Dto
{
    public class ApplicantInfoDto
    {

        public int Ctr { get; set; }

        public int Case_ID { get; set; }

        public int Actor_ID { get; set; }

        public string Name1 { get; set; }

        public string Name3 { get; set; }

        public string Full_Name { get; set; }

        public string Full_Name_Original { get; set; }

        public string Address { get; set; }

        public string Zip_Code { get; set; }

        public string State_ID { get; set; }

        public string Address { get; set; }

        public string Zip_Code { get; set; }

        public string State_ID { get; set; }

        public string Match { get; set; }
    }
}

数据库上下文文件:

        public DbSet<ApplicantInfo> ApplicantInfos { get; set; }

界面

using System;
using AppleApi.Models;

namespace AppleApi.Interfaces
{
    public interface IApplicantInfoRepository
    {
        Task<List<ApplicantInfo>> GetAllAsync();
        Task<ApplicantInfo> GetApplicantInfoAsync(string Number);
        Task<ApplicantInfo> CreateApplicantInfoAsync(ApplicantInfo ApplicantInfo);      
        
    }
}

和存储库

    using System;
using AppleApi.Interfaces;
using AppleApi.Models;
using AutoMapper;
using Newtonsoft.Json;

namespace AppleApi.Repository
{
    public class ApplicantRepository : IApplicantInfoRepository
    {

        private IConfiguration _configuration;
        private IWebHostEnvironment _env;
        private readonly HttpClient _client;


        public WipoApplicantRepository(IConfiguration configuration, IWebHostEnvironment webHostEnvironment, HttpClient client)
        {
            this._configuration = configuration;
            this._env = webHostEnvironment;
            this._client = client;
        }

       

        

        public async Task<ApplicantInfo> GetApplicantInfoAsync(string Number)
        {
            string baseUrl = _configuration.GetSection("BaseAPI").GetSection("baseURL").Value;

            var httpResponse = await _client.GetAsync($"{baseUrl}{Number}");

            if (!httpResponse.IsSuccessStatusCode)
            {
                throw new Exception("Cannot retrieve tasks");
            }

            var content = await httpResponse.Content.ReadAsStringAsync();
            var ApplicantInfo = JsonConvert.DeserializeObject<ApplicantInfo>(content);

            return ApplicantInfo;
        }
    }
}

最后是控制器

using System;
using System.Web.Http.Cors;
using AppleApi.Dto;
using AppleApi.Interfaces;
using AAppleApi.Models;
using AutoMapper;
using Microsoft.AspNetCore.Mvc;

namespace AppleApi.Controllers
{

    [Route("api/[controller]")]
    [ApiController]
    [EnableCors(origins: "https://localhost:7059/", headers: "*", methods: "*")]
    public class ApplicantInfoController : Controller
    {

        private readonly IApplicantInfoRepository _ApplicantInfoRepository;
        private readonly IMapper _mapper;

        public ApplicantInfoController(IApplicantInfoRepository ApplicantInfoRepository, IMapper mapper)
        {
            this._ApplicantInfoRepository =  ApplicantInfoRepository;
            this._mapper = mapper;
        }



        [HttpGet("{Number}")]
        [ProducesResponseType(200, Type = typeof(ApplicantInfo))]
        [ProducesResponseType(400)]
        public IActionResult GetApplicantInfo(string Number)
        {



            

            var ApplicantInfo = _mapper.Map<ApplicantInfoDto>(_ApplicantInfoRepository.GetApplicantInfoAsync(Number));

            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            return Ok(ApplicantInfo);
        }


    }
}

映射配置文件代码如下:

using System;
using AppleApi.Dto;
using AppleApi.Models;
using AutoMapper;

namespace AppleApi.Helper
{
    public class MappingProfiles : Profile
    {
        public MappingProfiles()
        {
            
            CreateMap<ApplicantInfo, ApplicantInfoDto>();
            CreateMap<ApplicantInfoDto, ApplicantInfo>();

        }

    }
}

这是我的第一个 .net 应用程序,如果我犯了一些愚蠢的错误,我会理解的。

您错过了 await

_mapper.Map<ApplicantInfoDto>(await _ApplicantInfoRepository.GetApplicantInfoAsync(Number));

exception.ToString() 拥有诊断问题所需的所有信息。

存储库响应类型中的 GetApplicantInfoAsync 方法是 Task<ApplicantInfo>。 因此,您需要使用 await 调用该方法,并确保 GetApplicantInfo 方法 async Task<IActionResult>:

[HttpGet("{Number}")]
[ProducesResponseType(200, Type = typeof(ApplicantInfo))]
[ProducesResponseType(400)]
public async Task<IActionResult> GetApplicantInfo(string Number)
{
    var ApplicantInfo = _mapper.Map<ApplicantInfoDto>(await _ApplicantInfoRepository.GetApplicantInfoAsync(Number));

    if (!ModelState.IsValid)
        return BadRequest(ModelState);

    return Ok(ApplicantInfo);
}