0
votes

I am following this setup below in the AutoMapper setup article but I keep getting this error. Is the documentation incorrect?

private static IMapper GetMapper(Container container)
{
    var mp = container.GetInstance<MapperProvider>();
    return mp.GetMapper();
}

public static void Register(Container container)
{
    container.RegisterSingleton(() => GetMapper(container));
    ...

MapperProvider.cs

public class MapperProvider
{
    private readonly Container _container;

    public MapperProvider(Container container)
    {
        _container = container;
    }

    public IMapper GetMapper()
    {
        var config = new MapperConfigurationExpression();
        config.ConstructServicesUsing(_container.GetInstance);
        config.AllowNullDestinationValues = true;
        config.AllowNullCollections = true;

        config.CreateMap<TeamModel, PoolTeamExtendedModel>();
        var mc = new MapperConfiguration(config);
        mc.AssertConfigurationIsValid();

        IMapper m = new Mapper(mc, t => _container.GetInstance(t));

        return m;
    }

Error

SimpleInjector.ActivationException: 'No registration for type MapperProvider could be found. Make sure MapperProvider is registered, for instance by calling 'Container.Register<MapperProvider>();' during the registration phase. An implicit registration could not be made because Container.Options.ResolveUnregisteredConcreteTypes is set to 'false', which is now the default setting in v5. This disallows the container to construct this unregistered concrete type. For more information on why resolving unregistered concrete types is now disallowed by default, and what possible fixes you can apply, see https://simpleinjector.org/ructd. '

1
"Is the documentation incorrect?" Yep, this example doesn't work with v5. I created a pull request on the documentation. - Steven

1 Answers

1
votes

I got it working with adding this concrete type first

container.RegisterSingleton<MapperProvider>();