I'm manually registrering a subset of my project's Web API controllers:
container.Register(typeof(ILGTWebApiController), controllerType, Lifestyle.Transient);
Works fine. However, when I run:
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
It seems to affect all Web API controllers in the project. I would like to leave the other ones untouched by simple injector.
If I don't run the code above, simple injector will complain about my controllers not having an empty constructor (which they obviously won't, since I'm using DI).
Instead of replacing the IDependencyResolver
, create a custom IHttpControllerActivator
that resolves the controller or fallbacks to the original activator otherwise:
public sealed class MyControllerActivator : IHttpControllerActivator
{
private readonly Container container;
private readonly IHttpControllerActivator original;
public MyControllerActivator(Container container, IHttpControllerActivator original)
{
this.container = container;
this.original = original;
}
public IHttpController Create(
HttpRequestMessage req, HttpControllerDescriptor desc, Type type)
{
if (type == typeof(ILGTWebApiController))
return (IHttpController)this.container.GetInstance(type);
return this.original.Create(req, desc, type);
}
}
You can configure your custom MyControllerActivator
as follows:
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator),
new MyControllerActivator(
container,
GlobalConfiguration.Configuration.Services.GetHttpControllerActivator()));