Mike Flynn
Mike Flynn

Reputation: 24325

Simple Injector and Mapster Dependency Injection setup

The documentation on the Mapster Wiki isnt very good and I cant seem to get it hooked up with Simple Injector.

I have this below:

container.RegisterSingleton(() =>
   MapperProvider.GetConfiguredMappingConfig());
container.Register<IMapper, ServiceMapper>(Lifestyle.Singleton);

public class MapperProvider
{
    public static TypeAdapterConfig GetConfiguredMappingConfig()
    {
        var config = new TypeAdapterConfig();
        return config;
    }
}

Error

System.InvalidOperationException: The configuration is invalid. Creating the instance for type BoutsController failed. The constructor of type ServiceMapper contains the parameter with name 'serviceProvider' and type IServiceProvider, but IServiceProvider is not registered. For IServiceProvider to be resolved, it must be registered in the container.

StackTrace:

at SimpleInjector.InstanceProducer.VerifyExpressionBuilding()
at SimpleInjector.Container.VerifyThatAllExpressionsCanBeBuilt(InstanceProducer[] producersToVerify)
at SimpleInjector.Container.VerifyThatAllExpressionsCanBeBuilt()
at SimpleInjector.Container.VerifyInternal(Boolean suppressLifestyleMismatchVerification)
at SimpleInjector.Container.Verify()
at Tournaments.MvcApplication.Application_Start() in F:\Global.asax.cs:line 339

Upvotes: 0

Views: 1266

Answers (1)

Steven
Steven

Reputation: 172606

I'm not familiar with Mapster, but after peeking at the ServiceMapper source code, I think the following code allows you to integrate with Mapster:

var config = new TypeAdapterConfig();
container.RegisterInstance<IMapper>(new ServiceMapper(container, config));

Upvotes: 1

Related Questions