3

Error: No parameterless constructor for AutoMapperConfiguration

I am using the nuget package automapper DI

public class AutoMapperConfiguration : Profile
{
    private readonly ICloudStorage _cloudStorage;

    public AutoMapperConfiguration(ICloudStorage cloudStorage)
    {
        _cloudStorage = cloudStorage;

        // Do mapping here
    }
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ICloudStorage, AzureStorage>();
    services.AddAutoMapper(); // Errors here
}

How do I use the automapper DI with parameters?

4
  • maybe this helps Commented May 28, 2017 at 17:08
  • 1
    @Dr.Fre Doesn't allow parameters in automapper constructor Commented May 28, 2017 at 18:11
  • @MartinDawson is right. You can only inject into custom resolvers and converters. Commented May 30, 2017 at 21:42
  • A complete answer with an example click this link Commented May 25, 2019 at 6:11

3 Answers 3

5

I don't think you are able to add DI parameters to Profiles. Part of the logic behind this may be that these are only instantianted once, so services registered through AddTransient would not behave as expected.

One option would be to inject it into an ITypeConverter:

public class AutoMapperConfiguration : Profile
{
    public AutoMapperConfiguration()
    {
        CreateMap<SourceModel, DestinationModel>().ConvertUsing<ExampleConverter>();
    }
}

public class ExampleConverter : ITypeConverter<SourceModel, DestinationModel>
{
    private readonly ICloudStorage _storage;

    public ExampleCoverter(ICloudStorage storage)
    {
        // injected here
        _storage = storage;

    }
    public DestinationModel Convert(SourceModel source, DestinationModel destination, ResolutionContext context)
    {
        // do conversion stuff
        return new DestinationModel();
    }
}

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<ICloudStorage, AzureStorage>();
    services.AddAutoMapper();
}
Sign up to request clarification or add additional context in comments.

2 Comments

indeed it's not recommended to inject into profiles: jimmybogard.com/automapper-usage-guidelines
Will, so we do not need to add anything else to ConfigureServices to setup DI for Automapper? I tried the same code as you had shown but Automapper failed to resolve the typeconverter.
2

you may want to try this on your Startup.cs, if AddAutoMapper is an an extension that you built, then add the code below to your extension.

 public void ConfigureServices(IServiceCollection services)
 {
    var mapperConfiguration = new MapperConfiguration(mc =>
    {
        IServiceProvider provider = services.BuildServiceProvider();
        mc.AddProfile(new AutoMapperConfiguration (provider.GetService<ICloudStorage>()));
    });

   services.AddSingleton(mapperConfiguration.CreateMapper());
  }

3 Comments

services.AddAutoMapper is a method from the NuGet package linked in the question. nuget.org/packages/…
am not sure, cause I use automapper extensively on all my .net core projects, and I did not or have not included any such statements.. To configure automapper, all i have/had to do are those few lines above, that said I inject IMapper into my class's constructor there after to use the same.
I'm simply answering the "if AddAutoMapper is an an extension that you built" part - we know that it isn't a custom-built one in this case.
-1

I found one solution to resolve this.

Create one list of types before add profile and pass it in the parameter.

public class AutoMapperConfiguration : Profile
{
    private readonly ICloudStorage _cloudStorage;

    public AutoMapperConfiguration(ICloudStorage cloudStorage)
    {
        _cloudStorage = cloudStorage;

        // Do mapping here
    }
}

public void ConfigureServices(IServiceCollection services)
{
    var types = new List<Type>();
    services.AddSingleton<ICloudStorage, AzureStorage>();
    services.AddAutoMapper((provider, cfg) =>
    {
        var storage = new AutoMapperConfiguration(provider.GetService<ICloudStorage>());

        types.Add(storage.GetType());
        cfg.AddProfile(storage);
        //others profiles

    }, types);
}

1 Comment

check this out: jimmybogard.com/automapper-usage-guidelines. it's not recommended.

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.