.Net - Problems when using AutoMapper in WebApi

0

I am trying to mount AutoMapper on my WebApi but it gives me an error. I have loaded the libraries with nuget and I have put the following lines.

In the WebApiConfig.cs file I include the following:

Mapper.Initialize(cfg =>
{
    cfg.AddProfile<App_Start.AutoMapperConfig>();
    cfg.AllowNullDestinationValues = false;
});

and the AutoMapperConfig class contains the following code:

public class AutoMapperConfig : Profile
{
    public static void Initialize()
    {
        Mapper.Initialize((config) =>
        {
            config.CreateMap<Model.Client, DTO.ClientBasic>();
        });
    }
}

This is my domain class:

public class Client
{
    [Key]
    public Guid Id { get; set; }
    [Required]
    public string Secret { get; set; }
    [Required]
    [MaxLength(100)]
    public string Name { get; set; }
    [Required]
    [MaxLength(10)]
    [Index("Client_ClientCode", IsUnique = true)]
    public string ClientCode { get; set; }
    public ApplicationTypes ApplicationType { get; set; }
    public bool Active { get; set; }
    public int RefreshTokenLifeTime { get; set; }
    [MaxLength(100)]
    public string AllowedOrigin { get; set; }
    public Guid CreatorId { get; set; }

    public ICollection<RefreshToken> RefreshToken { get; set; }
    public ICollection<Role> Role { get; set; }
    [ForeignKey("CreatorId")]
    public User Creator { get; set; }
}

And this my DTO:

public class ClientBasic
{
    public Guid Id { get; set; }
    public string Secret { get; set; }
    public string Name { get; set; }
    public string ClientCode { get; set; }
    public int ApplicationType { get; set; }
    public bool Active { get; set; }
    public int RefreshTokenLifeTime { get; set; }
    public string AllowedOrigin { get; set; }
    public Guid CreatorId { get; set; }
}

I make the call to the Mapper from my WebApi method to transform the database Client object into the object I am going to return:

List<Model.Client> clients = _ClientService.SelectAll().ToList();
return this.Ok(AutoMapper.Mapper.Map<List<DTO.ClientController.ClientBasic>>(clients));

But it returns the following error:

{Message: "Error.",…}
ExceptionMessage
:
"Error mapping types.
↵
↵Mapping types:
↵List'1 -> List'1
↵System.Collections.Generic.List'1[[MGA.Model.Client, MGA.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.List'1[[MGA.WebApi.DTO.ClientController.ClientBasic, MGA.WebApi.DTO, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]"
ExceptionType
:
"AutoMapper.AutoMapperMappingException"
InnerException
:
{Message: "Error.",…}
ExceptionMessage
:
"↵Unmapped members were found. Review the types and members below.↵Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type↵For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters↵================================================================
↵AutoMapper created this type map for you, but your types cannot be mapped using the current configuration.
↵Client -> ClientBasic (Destination member list)
↵MGA.Model.Client -> MGA.WebApi.DTO.ClientController.ClientBasic (Destination member list)
↵
↵Unmapped properties:
↵SecretText
↵"
ExceptionType
:
"AutoMapper.AutoMapperConfigurationException"
Message
:
"Error."
StackTrace
:
"   en AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable'1 typeMaps)
↵   en AutoMapper.MapperConfiguration.AssertConfigurationIsValid(TypeMap typeMap)
↵   en lambda_method(Closure , List'1 , List'1 , ResolutionContext )"
Message
:
"Error."
StackTrace
:
"   en lambda_method(Closure , List'1 , List'1 , ResolutionContext )
↵   en lambda_method(Closure , Object , Object , ResolutionContext )
↵   en AutoMapper.Mapper.AutoMapper.IMapper.Map[TDestination](Object source)
↵   en AutoMapper.Mapper.Map[TDestination](Object source)
↵   en MGA.WebApi.Controllers.ClientController.Get() en C:\Proyectos\MGA\MGA.WebApi\Controllers\ClientController.cs:línea 45
↵   en lambda_method(Closure , Object , Object[] )
↵   en System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.<GetExecutor>b__3(Object instance, Object[] methodParameters)
↵   en System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
↵   en System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary'2 arguments, CancellationToken cancellationToken)
↵--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---
↵   en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵   en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵   en System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__1.MoveNext()
↵--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---
↵   en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵   en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵   en System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()
↵--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---
↵   en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵   en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵   en System.Web.Http.Filters.AuthorizationFilterAttribute.<ExecuteAuthorizationFilterAsyncCore>d__3.MoveNext()
↵--- Fin del seguimiento de la pila de la ubicación anterior donde se produjo la excepción ---
↵   en System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
↵   en System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
↵   en System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()"

Does anyone know what may be happening ???

    
asked by Alejandro 22.08.2018 в 09:47
source

0 answers