I'm using simple injector with my MVC application. I'm injecting my Entity Framework DbContext into my MVC Controllers but one specific controller needs to initialize the DbContext with optional arguments (e.g ownsConnection:false).
Can I register multiple times the same interface with different constructor arguments? How would I achieve this?
public static void Initialize()
{
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
InitializeContainer(container);
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
private static void InitializeContainer(Container container)
{
container.Register<IDbContext>(() => new DbContext("ConnectionString"), Lifestyle.Scoped);
}
For a specific controller I need to inject DbContext with argument contextOwnsConnection:false
You can decorate your controller to customize its resolution. But you cannot do tha directly. Look here, that refers to this SO question: How to decorate an ASP.NET MVC controller with Simple Injector.
Then have a look at this technique Intercepting the Creation of Types