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?

Martin Dawson
  • 7,455
  • 6
  • 49
  • 92

3 Answers3

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();
}
Will Ray
  • 10,621
  • 3
  • 46
  • 61
  • indeed it's not recommended to inject into profiles: https://jimmybogard.com/automapper-usage-guidelines/ – lnaie May 06 '20 at 08:43
  • 1
    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. – Rizwan Sep 16 '22 at 09:46
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());
  }
Jaya
  • 3,721
  • 4
  • 32
  • 48
  • 1
    `services.AddAutoMapper` is a method from the NuGet package linked in the question. https://www.nuget.org/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/ – Will Ray Jun 02 '17 at 04:55
  • 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. – Jaya Jun 03 '17 at 19:08
  • 1
    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. – Will Ray Jun 05 '17 at 00:25
-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);
}
  • check this out: https://jimmybogard.com/automapper-usage-guidelines/. it's not recommended. – lnaie May 06 '20 at 08:42