I am using the AutoMapper in .NET CORE application. I configured the AutoMapper in my Startup.cs in ConfigureServices.cs method file which is as follows :
public void ConfigureServices(IServiceCollection services)
{
/* Some Code */
/* AutoMapper Configuration */
services.AddAutoMapper(typeof(AutoMapperProfileConfiguration).GetType().Assembly);
services.AddMvcCore();
/* Some Code */
}
and the AutoMapperProfileConfiguration is as follows :
public class AutoMapperProfileConfiguration : Profile
{
public AutoMapperProfileConfiguration()
{
CreateMap<LoginRequestDto, Users>();
}
}
Then I used this mapper in the following class :
public LoginResponseDto CreateAnAccount(LoginRequestDto loginRequestDto)
{
var userInfo = _mapper.Map<Users>(loginRequestDto);
userInfo.Id = Guid.NewGuid();
_context.InsertItem<Users>(userInfo);
var insertedUserDetails = _context.GetItemById<Users>(userInfo.Id);
insertedUserDetails.Wait();
return _mapper.Map<LoginResponseDto>(insertedUserDetails.Result);
}
But it gives me the following exception when I try to map the data present in loginRequestDto to Users class :
AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
Object -> Users
System.Object -> NeighborhoodHelpers.UserMicroservice.Entities.Models.Users
at lambda_method(Closure , Object , Users , ResolutionContext )
at NeighborhoodHelpers.UserMicroservice.DataAccessProvider.UserDataAccess.UserDataAccess.CreateAnAccount(LoginRequestDto loginRequestDto) in D:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.DataAccessProvider\UserDataAccess\UserDataAccess.cs:line 29
at NeighborhoodHelpers.UserMicroservice.Services.UserServices.UserService.CreateAnAccount(LoginRequestDto loginRequestDto) in D:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.Services\UserServices\UserService.cs:line 22
at NeighborhoodHelpers.UserMicroservice.API.Controllers.LoginController.CreateAnAccount(LoginRequestDto loginRequestDto) in D:\Gursimran\Hackathon2\NeighborhoodHelpers.UserMicroservice.API\NeighborhoodHelpers.UserMicroservice.API\Controllers\LoginController.cs:line 34
at lambda_method(Closure , Object , Object[] )
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
I am using .NET Core 3.1. Please help me with this. I am unable to use the AutoMapper Functionality.
In your code, looks like the AutoMapper's configuration is not seeing the defined profile, in order to correct it, I recommend using one of the two options below.
Option 1: NuGet Package AutoMapper
Startup.csCustomAutoMapper.cs :
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
namespace MyApp.CustomConfiguration
{
public static class CustomAutoMapper
{
public static void AddCustomConfiguredAutoMapper(this IServiceCollection services)
{
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new AutoMapperProfileConfiguration());
});
var mapper = config.CreateMapper();
services.AddSingleton(mapper);
}
}
}
In Startup.cs:
services.AddCustomConfiguredAutoMapper();
Option 2: Use AutoMapper.Extensions.Microsoft.DependencyInjection Nuget Package (I personally prefer to use it for more than two years as I use multiple assemblies in my Solution and this package gives automatic discovery for Profiles as it automatically scans the solution for any class inherit from AutoMapper's Profile class.
Simply in your Startup.cs :
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
Change your Startup.cs like below:
services.AddAutoMapper(typeof(AutoMapperProfileConfiguration));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With