TLTR: the problem is caused by the implicit way Web API handles resolving controller types; register your Web API controllers explicitly and you’ll see where the problem is.
Here is a step by step what is happening under the covers:
System.Web.Http.DefaultHttpControllerActivatorcalls into the
SimpleInjectorWebApiDependencyResolverand requests the creation of an API controller.
SimpleInjectorWebApiDependencyResolverforwards that call to the
Containerinstance however, does not have any explicit registrations for that API Controller (since you supplied an empty container to the resolver).
- Since there is no explicit registration, the container tries to do a last minute registration for that type.
- That Controller type however depends on interfaces that can’t be resolved because they are not registered in the container (remember, your container is empty).
- Although the container would normally throw an exception, null is returned in this case, because the type is requested through the
IServiceProvider.GetServicemethod and the type was not registered explictly.
GetServicemethod will return
nullas well, since it’s by definition that it should return null; It should return null when no registration exists (which currently is the case).
- Since the
DefaultHttpControllerActivatorwill fall back to its default behavior, which means creating that type itself, but this requires the controller to have a default constructor.
Long story short, the problem is caused by the implicit way Web API handles resolving controller types.
So the solution here is to:
- Have only one single
Containerin your web application. This prevents all sorts of trouble and complication of your configuration.
- Register all Web API Controllers explicitly in the container. Registering controllers explicitly will ensure that Simple Injector will throw an exception when a controller can’t be resolved. Besides, this allows you to call
container.Verify()which will make the application fail during startup when the configuration is invalid (a verifiable configuration is important). And this also allows you to diagnose the configuration which gives you even more confidence about the correctness of your configuration.
My advice is to place MVC and Web API in their own project. This will make things much easier.
Registering all Web API controllers can be done with the following code:
Because this error is so common, newer versions of the
SimpleInjectorWebApiDependencyResolver class will simply never return
null when a controller type is requested. Instead it will throw a descriptive error. Because of this you should never see error anymore, as long as you use the official