AutoMapper: Missing type map configuration or unsupported mapping in .Net Core
Dung Do Tien
Dec 30 2020
700
I use AutoMapper for web API application of asp.net core currently. I am getting the following error:
Mapping types:
Object -> CustomerDto
System.Object -> CRMAPI.Application.EzCustomers.Customers.Queries.CustomerDto
at lambda_method(Closure , Object , CustomerDto , ResolutionContext )
at Shared.Extensions.MapperExtension.To[T](Object source) in D:\Project\Ez-buy\crm-api\Shared\Extensions\MapperExtension.cs:line 17
at CRMAPI.Application.EzCustomers.Customers.Queries.GetListCustomerQueryHandler.<>c.<Handle>b__2_2(Customer c) in D:\Project\Ez-buy\crm-api\CRMAPI.Application\EzCustomers\Customers\Queries\GetListCustomerQueryHandler.cs:line 58
at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
And below is my code.
MapperExtension.cs
public static class MapperExtension
{
private static IMapper _mapper;
public static IMapper RegisterMap(this IMapper mapper)
{
_mapper = mapper;
return mapper;
}
public static T To<T>(this object source)
{
return _mapper.Map<T>(source);
}
public static T To<T>(this object source, T dest)
{
return _mapper.Map(source, dest);
}
}
GetCustomerDtoByAccountIdQueryHandler.cs
public class GetCustomerDtoByAccountIdQuery : IRequest<CustomerDto> { public GetCustomerDtoByAccountIdQuery(string accountId) { AccountId = accountId; } public string AccountId { get; private set; } } public class GetCustomerDtoByAccountIdQueryHandler : IRequestHandler<GetCustomerDtoByAccountIdQuery, CustomerDto> { private readonly IRepository<Customer> _repository; public GetCustomerDtoByAccountIdQueryHandler(IRepository<Customer> repository) { _repository = repository; } public async Task<CustomerDto> Handle(GetCustomerDtoByAccountIdQuery request, CancellationToken cancellationToken) { var customer = await _repository.GetSingleAsync(x => x.AccountId.Equals(request.AccountId)); var response = customer.To<CustomerDto>(); return response; } }
Please suggest to me how to solve this problem.
Have 1 answer(s) found.
-
M1
Milan cubes Dec 30 2020
Please try to check the Automapper configuration in your application, you can refer to my configuration as follows:
DomainToDtoMappingProfile.cs:
public class DomainToDtoMappingProfile : Profile { public DomainToDtoMappingProfile() { CreateMap<Order, OrderDto>(); } }
Startup.cs:
var mapperConfig = new MapperConfiguration(cfg => { cfg.AddProfile(new DomainToDtoMappingProfile()); }); services.AddSingleton(mapperConfig.CreateMapper().RegisterMap());
Please refer to Auto Mapper configuration here
* Type maximum 2000 characters.
* All comments have to wait approved before display.
* Please polite comment and respect questions and answers of others.