Open Closed

ObjectMapper throwing exception #4555


User avatar
0
moinahmed created

I have added an object mapper in my custom module as below:

public class Auth0UsersManagementModuleAutoMapperProfile : Profile
    {
        public Auth0UsersManagementModuleAutoMapperProfile()
        {
            CreateMap<UserDto, UserCreateRequest>()                
              //.ForMember(dest => dest.Prop, opt => opt.Ignore())
              //.ForMember(dest => dest.Prop, opt => opt.MapFrom(src => src.Prop))
                .AfterMap((src, dest) => {
                    dest.Connection = "Username-Password-Authentication";
                });

            CreateMap<UserDto, UserUpdateRequest>()
                .ForMember(dest => dest.Password, opt => opt.Ignore())
                .AfterMap((src, dest) => {
                    dest.Connection = "Username-Password-Authentication";
                });

            CreateMap<User, UserDto>()
                .ForMember(dest => dest.Password, opt => opt.Ignore())
                .ForMember(dest => dest.Id,       opt => opt.MapFrom(src => src.UserId));
            
            CreateMap<User, UserUpdateRequest>();
        }
    }

The code was working fine and now suddently have stopped working and throwing exception as: "AutoMapperMappingException: Missing type map configuration or unsupported mapping."

Really annoying to see nonderministic code behavior. I have been using AutoMapper since quite some and I'm 100% sure about the mapping is there in code, however, still no luck.


7 Answer(s)
  • User Avatar
    0
    salih created
    Support Team .NET Developer

    Hello, have you looked here? Configuration may be missing.

  • User Avatar
    0
    moinahmed created

    Yes, already added; as I said it was working like a charm and then suddenly stopped working.

  • User Avatar
    0
    salih created
    Support Team .NET Developer

    Can you share where it gives error?

  • User Avatar
    0
    moinahmed created

  • User Avatar
    0
    salih created
    Support Team .NET Developer

    Hello, I can't comment because I don't know the content of your classes. There may be a map you forgot to add. Can you share the source code?

  • User Avatar
    0
    moinahmed created

    I think you must be interested in the two files:

    public class Auth0UsersManagementModule : AbpModule
        {
            public override void ConfigureServices(ServiceConfigurationContext context)
            {
                var configuration = context.Services.GetConfiguration();
    
                Configure<Auth0ConnectionOptions>(
                    configuration.GetSection("Auth0"));
            }
    
            public override void OnApplicationInitialization(ApplicationInitializationContext context)
            {
                var service = context
                    .ServiceProvider
                    .GetRequiredService<Auth0UsersManagementInit>();
                service.Initialize();
            }
        }
    
    public class Auth0UsersManagementModuleAutoMapperProfile : Profile
        {
            public Auth0UsersManagementModuleAutoMapperProfile()
            {
                CreateMap<UserDto, UserCreateRequest>()                
                  //.ForMember(dest => dest.Prop, opt => opt.Ignore())
                  //.ForMember(dest => dest.Prop, opt => opt.MapFrom(src => src.Prop))
                    .AfterMap((src, dest) => {
                        dest.Connection = "Username-Password-Authentication";
                    });
    
                CreateMap<UserDto, UserUpdateRequest>()
                    .ForMember(dest => dest.Password, opt => opt.Ignore())
                    .AfterMap((src, dest) => {
                        dest.Connection = "Username-Password-Authentication";
                    });
    
                CreateMap<User, UserDto>()
                    .ForMember(dest => dest.Password, opt => opt.Ignore())
                    .ForMember(dest => dest.Id,       opt => opt.MapFrom(src => src.UserId));
                
                CreateMap<User, UserUpdateRequest>();
            }
        }
    

    Mapping is called as: var request = ObjectMapper.Map<UserDto, UserCreateRequest>(user);

  • User Avatar
    0
    salih created
    Support Team .NET Developer

    Hello, have you looked here? Configuration may be missing.

    Your code does not have configuration

    Configure<AbpAutoMapperOptions>(options =>
    {
        //Add all mappings defined in the assembly of the MyModule class
        options.AddMaps<MyModule>();
    });
    
Made with ❤️ on ABP v8.2.0-preview Updated on března 25, 2024, 15:11