4

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.

1
  • My answer has been updated Commented Dec 28, 2020 at 12:27

3 Answers 3

4

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

  1. For Flexibility and Cleaner Code, define an extension method to add the current AutoMapperProfileConfiguration and many others as needed in the future.
  2. Call it in Startup.cs

CustomAutoMapper.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());
Sign up to request clarification or add additional context in comments.

4 Comments

@Simran Kaur How many assemblies you have in your solution? What is the name of the package of AutoMapper and the version you have installed?
I have installed Automapper version 10.1.1
How many projects in your solution? and where is the Profile class is located (in which assembly?) @Simran Kaur
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); solved my issue! Thanks a lot.
3

Change your Startup.cs like below:

services.AddAutoMapper(typeof(AutoMapperProfileConfiguration));

Comments

-1

Seeing the properties of Users and LoginRequestDto can make it easier to answer your question. Because as can be understood from the error message, there are properties that need to be "ignore" or you are doing things like converting the dictionary to a list.

If you can show your classes, maybe we can arrange the following line accordingly and solve the problem.

CreateMap<LoginRequestDto, Users>();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.