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?
The MVC integration includes an Autofac module that will add HTTP request lifetime scoped registrations for the web abstraction classes. This will allow you to put the web abstraction as a dependency in your class and get the correct value injected at runtime.
Autofac is the most widely used DI/IoC container for ASP.NET, and it is fully compatible with.NET Core as well. . NET Core has a built-in dependency injection framework that is ready to use. Even though the default DI may provide sufficient functionality, there are several limitations when using it.
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.
var builder = new ContainerBuilder(); // Add your registrations var container = builder.Build(); // Set the dependency resolver for Web API. var webApiResolver = new AutofacWebApiDependencyResolver(container); GlobalConfiguration.Configuration.DependencyResolver = webApiResolver; // Set the dependency resolver for MVC. var mvcResolver = new AutofacDependencyResolver(container); DependencyResolver.SetResolver(mvcResolver);
It is also possible to share registrations between the two because the InstancePerApiRequest
and InstancePerHttpRequest
lifetime 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.DependencyResolver
and MVC uses DependencyResolver.SetResolver
.
I thought I'd add a little help those struggling with this in mvc5 and web api 2.
First add nuget packages
in global add in application_start (or as app_start class) add call to the below class
AutofacConfig.RegisterAutoFac();
now add this class under App_start
using System.Reflection; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Mvc; using Autofac; using Autofac.Integration.Mvc; using Autofac.Integration.WebApi; namespace Example1.Web { public class AutofacConfig { public static IContainer RegisterAutoFac() { var builder = new ContainerBuilder(); AddMvcRegistrations(builder); AddRegisterations(builder); var container = builder.Build(); DependencyResolver.SetResolver(new AutofacDependencyResolver(container)); GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container); return container; } private static void AddMvcRegistrations(ContainerBuilder builder) { //mvc builder.RegisterControllers(Assembly.GetExecutingAssembly()); builder.RegisterAssemblyModules(Assembly.GetExecutingAssembly()); builder.RegisterModelBinders(Assembly.GetExecutingAssembly()); builder.RegisterModelBinderProvider(); //web api builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired(); builder.RegisterModule<AutofacWebTypesModule>(); } private static void AddRegisterations(ContainerBuilder builder) { //builder.RegisterModule(new MyCustomerWebAutoFacModule()); } } }
From now for each new assembly you add to the project add a new module and then register the module in the AddRegisterations function (example given)
Note:
I returned the container, this isn't necessary.
This scans the current assembly for modules so don't add local modules in AddRegisterations otherwise you will register everything twice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With