I can not initialize Automapper 6.2.2 Settings Asp.netcore 2.0

-1

In my Application layer, I have the following classes:

public class AutoMapperConfig
    {

        public static MapperConfiguration RegisterMappings()
        {
            return new MapperConfiguration(cfg =>
            {
                cfg.AddProfile(new DomainToViewModelMappingProfile());
                cfg.AddProfile(new ViewModelToDomainMappingProfile());
            });
        }
    }

public class DomainToViewModelMappingProfile : Profile
    {
        public DomainToViewModelMappingProfile()
        {
            CreateMap<Customer, CustomerViewModel>();
        }
    }

public class ViewModelToDomainMappingProfile : Profile
    {
        public ViewModelToDomainMappingProfile()
        {
            CreateMap<CustomerViewModel, RegisterNewCustomerCommand>()
                .ConstructUsing(c => new RegisterNewCustomerCommand(c.Name, c.Email, c.BirthDate));
            CreateMap<CustomerViewModel, UpdateCustomerCommand>()
                .ConstructUsing(c => new UpdateCustomerCommand(c.Id, c.Name, c.Email, c.BirthDate));
        }
    }

In my Asp.Net MVC layer, I have the Startup.cs class which through the services.AddAutoMapper () method; initializes my Automapper configuration classes, except that it calls the method and does not pass through the classes mentioned above. Consequently, when I have to save some registry in my bank, an error occurs in my controller related to Automapper:

Error: "+ $ exception {AutoMapper.AutoMapperConfigurationException: 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

CustomerViewModel - > RegisterNewCustomerCommand (Destination member list) SystemComercial.Application.ViewModels.CustomerViewModel - > SystemComercial.Domain.Commands.Customer.RegisterNewCustomerCommand (Destination member list)

Unmapped properties: Timestamp ValidationResult MessageType AggregateId

at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid (IEnumerable'1 typeMaps)    at lambda_method (Closure, CustomerViewModel, RegisterNewCustomerCommand, ResolutionContext)    at lambda_method (Closure, Object, Object, ResolutionContext)    at AutoMapper.Mapper.AutoMapper.IMapper.Map [TDestination] (Object source)    at SystemComercial.Application.Services.CustomerAppService.Register (CustomerViewModel customerViewModel) in C: \ Users \ jalbe \ Desktop \ New_Project \ CommerceSystem \ src \ CommerceSystem.Application \ Services \ CustomerAppService.cs: line 46    at SystemComercial.Posentation.Web.MVC.Controllers.CustomerController.Create (CustomerViewModel customerViewModel) in C: \ Users \ jalbe \ Desktop \ New_Project \ CommerceSystem \ src \ CommerceSystem.Presentation.Web.MVC \ Controllers \ CustomerController.cs: line 62    at lambda_method (Closure, Object, Object [])    at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute (Object target, Object [] parameters)    at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext ()} AutoMapper.AutoMapperConfigurationException "

 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
       

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
            services.AddAutoMapper(); //Jalber

            // Adding MediatR for Domain Events and Notifications
            services.AddMediatR(typeof(Startup)); //Jalber

            // .NET Native DI Abstraction
            RegisterServices(services); //Jalber
           

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseBrowserLink();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }

        private static void RegisterServices(IServiceCollection services)
        {
            // Adding dependencies from another layers (isolated from Presentation)
            NativeInjectorBootStrapper.RegisterServices(services);
        }
    }

How do I resolve this?

    
asked by anonymous 14.01.2018 / 18:25

2 answers

0

Hello @

I found the solution to the problem.

In the Startup.cs class, just swap:

services.AddAutoMapper ()

by:

services.AddAutoMapper ( Assembly.GetAssembly (typeof (AutoMapperConfig)) )

Here it worked for me. I hope I have helped!

    
11.04.2018 / 18:44
0

Since the version of the course of Eduardo Pires there have been changes in the Automapper, which make the configuration easier and readable. I'll demonstrate.

First create the mapping profile class, just like you did, but only one, since we will use the .ReverseMap() method, which already does the reverse mapping of objects automatically.

public class MapProfile:AutoMapper.Profile
    {
        public MapProfile()
        {
            // Core
            CreateMap<SchoolPrincipal, SchoolPrincipalViewModel>().ReverseMap();
        }
    }

Next, create the class that will start Automapper in class Startup

public class MapConfiguration
{
    public static void RegisterMapProfile()
    {
        Mapper.Initialize(c =>
        {
            c.AddProfile<MapProfile>();
        });
    }
}

Now in method ConfigureServices(IServiceCollection services) of class Startup include the line:

MapConfiguration.RegisterMapProfile();
    
04.05.2018 / 21:57