windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

AutoMapper with DI: Cannot create an instance of type… When using a ValueResolver

Asked By: Anonymous

I have a .NET Core 3.1 API with the following Nuget packages:

  • AutoMapper (10.1.1)
  • AutoMapper.Extensions.Microsoft.DependencyInjection (8.1.1)

I’m trying to map a value from an entity to a dto using a ValueResolver and I’m having an exception:

AutoMapperMappingException: Cannot create an instance of type TestAutomapperResolver.Mapping.CustomResolver

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddAutoMapper(cfg => cfg.AddMaps(typeof(TestProfile).Assembly));
}

TestProfile.cs

public class TestProfile : Profile
{
    public TestProfile()
    {
        CreateMap<TestEntity, TestDto>()
            .ForMember(src => src.PropertyToBeMappedByResolver, opts => opts.MapFrom<CustomResolver>());
    }
}

public class CustomResolver : IValueResolver<TestEntity, TestDto, string>
{
    public string Resolve(TestEntity source, TestDto destination, string destMember, ResolutionContext context)
    {
        return "String generated with resolver";
    }
}

When doing mapper.CreateMap<TestDto>(entity); I’m receiving that exception.

By the way, using this resolver as opts => opts.MapFrom(CustomResolver()) is not an option because I want to inject some service into that resolver.

Any idea?


Solution

Answered By: Anonymous

You’re using AddMaps when you shouldn’t be. AddMaps just adds profiles and mappings, but doesn’t add all the extra services that the DI package does.

This will do it properly:

services.AddAutoMapper(typeof(TestProfile).Assembly);

Now, AutoMapper gives you a very unhelpful error here, but the issue goes back to Microsoft Dependency injection. The DI doesn’t know about your custom resolver type, so it doesn’t even try.

Since you didn’t use the DI package’s extension method, the resolver doesn’t get added to the service collection. You can manually add these services if needed:

services.AddAutoMapper(cfg => cfg.AddMaps(typeof(TestProfile).Assembly));
services.AddTransient<CustomResolver>();

techinplanet staff

Porno Gratuit Porno Français Adulte XXX Brazzers Porn College Girls Film érotique Hard Porn Inceste Famille Porno Japonais Asiatique Jeunes Filles Porno Latin Brown Femmes Porn Mobile Porn Russe Porn Stars Porno Arabe Turc Porno caché Porno de qualité HD Porno Gratuit Porno Mature de Milf Porno Noir Regarder Porn Relations Lesbiennes Secrétaire de Bureau Porn Sexe en Groupe Sexe Gay Sexe Oral Vidéo Amateur Vidéo Anal

Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno
524 Origin Time-out

524 Origin Time-out


cloudflare-nginx