Is it possible to configure Autofac to work with ASP .NET MVC and ASP .NET Web Api. I’m aware that the dependency resolvers are different. But when using the documented approaches I can only set one global resolver.
// Set the dependency resolver implementation.
GlobalConfiguration.Configuration.DependencyResolver = resolver;
Is this approach bad idea? Should I separate my solution into two projects and handle the dependency injection for each individually?
It is certainly possible to configure Autofac to work with both MVC and Web API. This is expected to be a very common scenario. There are two separate dependency resolver implementations because MVC and Web API can be used independently of one another. The same applies for the Autofac integrations.
When using both MVC and Web API in the same application each will require its own dependency resolver, though they can be provided with the same instance of the container.
It is also possible to share registrations between the two because the
InstancePerApiRequestandInstancePerHttpRequestlifetime scopes now share the same tag.Note that the mechanism for setting the dependency resolver for Web API and MVC is different. Web API uses
GlobalConfiguration.Configuration.DependencyResolverand MVC usesDependencyResolver.SetResolver.