Automapper: ProjectTo<> Requires IConfigurationProvider Instance

Created on 17 Aug 2018  路  7Comments  路  Source: AutoMapper/AutoMapper

After upgrading to 7.0.1, ProjectTo<> now requires IConfigurationProvider instance.
(I understand the error message states this, and apparently it's been for a while, however my solution worked without it on 7.0.0. See https://stackoverflow.com/questions/41961439/mapper-not-initialized-when-use-projectto)

Now I've had to add this in numerous places, also injecting an IMapper into various controllers that didn't require it before (but were using ProjectTo). Is there no better way? Thanks.

(Apologies if this is not the correct forum, feel free to close but please comment.)

Most helpful comment

You can inject IConfigurationProvider directly if you don't need IMapper.

All 7 comments

If not perhaps the parameterless overload can be removed to catch this at compile time?

ProjectTo can work with the static configuration and then you don't have to pass anything. Maybe you were using the DI package with the static configuration, that's no longer supported. That was weird to begin with. More of a backward compatibility thing.

You can inject IConfigurationProvider directly if you don't need IMapper.

That's true. I'm using a (mapping) Profile and AutoMapper extensions for ASP.NET Core, so I've just been calling services.AddAutoMapper(); in ConfigureServices in Startup.cs to initialize it. Myabe it was a change since 4.0.1 of that package.

@brendonwm after 4.0.1 the static Mapper instance initialize has been removed from DI register method, you need to call Mapper.Initialize() manually.
However Mapper.Initialize method doesn't scan the AppDomain assemblies, it requires developer to offer the configuration, you can copy the scan configuration code from DI register method to create the static Mapper configuration.(That is what I did, it works but not graceful)

@brendonwm it was. Basically because we're integration with a DI container we actually need to do DI everywhere :) so I got rid of the static stuff in that package. It just caused too many problems.

Moral of the story: if you're going to do DI, then do it. Don't go halfway.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fruitbatinshades picture fruitbatinshades  路  3Comments

idotnetdev picture idotnetdev  路  4Comments

colin-young picture colin-young  路  6Comments

vadimshaporov picture vadimshaporov  路  7Comments

zolakt picture zolakt  路  4Comments