I cannot initialize Automapper Settings 6.2.2 Asp.netcore 2.0

Asked

Viewed 596 times

-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 that through the services.Addautomapper() method; initializes my Automapper configuration classes, only that it calls the method and does not go through the classes cited above. Consequently, when I send to save a record to my bank, there is an error in my Automapper-related controller:

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) Systemscommercial.Application.Viewmodels.Customerviewmodel -> Systemscommercial.domain.Commands.Customer.Registernewcustomercommand (Destination Member list)

Unmapped properties: Timestamp Validationresult Messagetype Aggregateid

at AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable`1 typeMaps) lambda_method(Closure , Customerviewmodel , Registernewcustomercommand , Resolutioncontext ) lambda_method(Closure , Object , Object , Resolutioncontext ) At automapper.Mapper.Automapper.IMapper.Map[Tdestination](Object source) at SistemaComercial.Application.Services.CustomerAppService.Register(CustomerViewModel customerViewModel) in C:\Users\jalbe\Desktop\New_Project\SistemaComercial\src\SistemaComercial.Application\Services\CustomerAppService.cs:line 46 at SistemaComercial.Presentation.Web.MVC.Controllers.CustomerController.Create(CustomerViewModel customerViewModel) in C:\Users\jalbe\Desktop\New_Project\SistemaComercial\src\SistemaComercial.Presentation.Web.MVC\Controllers\CustomerController.cs:line 62 lambat da_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 fix it?

2 answers

0

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

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

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

Then create the class that will start Automapper in the class Startup

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

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

MapConfiguration.RegisterMapProfile();
  • 1

    I noticed..... Very good!!!

0


Hello @

I found the solution to the problem.

In the Startup.Cs class, just switch:

services.Addautomapper()

for:

services.Addautomapper(Assembly.Getassembly(typeof(Automapperconfig)));

Here for me it worked. I hope I helped!

  • 1

    Thanks for the return Lauro! In my case, the problem was happening with another component (Automapper.Extensions.Microsoft.Dependencyinjection) that I was installing in the latest version and was giving... It was only I install version 2.0.1 that solved....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.