Autofac and ASP.NET Web API ApiController

I used autofac with MVC 3 for a while and love it. I recently upgraded the project to MVC 4 and everything seems to work except Api ApiControllers. I get the following exception.

An error occurred when trying to create a controller of type 'MyNamespace.Foo.CustomApiController'. Make sure that the controller has a parameterless public constructor. 

It seems to me that this is a problem with DI through autofac. I missed something or something at work. I know MVC4 has just been released and is in beta, so I do not expect much, but I realized that I could miss something.

+9
asp.net-web-api asp.net-mvc-4 autofac
Feb 26 '12 at 3:28
source share
2 answers

I released Autofac integration packages on NuGet for MVC 4 beta and Web API. Integration will create an Autofac lifecycle area for each controller request (MVC controller or API controller depending on integration). This means that the controller and its dependencies will be automatically placed at the end of each call. Both packages can be installed side by side in one project.

MVC 4

https://nuget.org/packages/Autofac.Mvc4

http://alexmg.com/post/2012/03/09/Autofac-ASPNET-MVC-4-(Beta)-Integration.aspx

Web interface

https://nuget.org/packages/Autofac.WebApi/

http://alexmg.com/post/2012/03/09/Autofac-ASPNET-Web-API-(Beta)-Integration.aspx

Links are now fixed.

+10
Mar 09 2018-12-12T00:
source share

I just configured this in one of my applications. There are different ways to do this, but I like this approach:

Autofac and ASP.NET Web API System.Web.Http.Services.ID Dependencies Resolver Integration

First, I created a class that implements the System.Web.Http.Services.IDependencyResolver interface.

 internal class AutofacWebAPIDependencyResolver : System.Web.Http.Services.IDependencyResolver { private readonly IContainer _container; public AutofacWebAPIDependencyResolver(IContainer container) { _container = container; } public object GetService(Type serviceType) { return _container.IsRegistered(serviceType) ? _container.Resolve(serviceType) : null; } public IEnumerable<object> GetServices(Type serviceType) { Type enumerableServiceType = typeof(IEnumerable<>).MakeGenericType(serviceType); object instance = _container.Resolve(enumerableServiceType); return ((IEnumerable)instance).Cast<object>(); } } 

And I have another class that contains my registrations:

 internal class AutofacWebAPI { public static void Initialize() { var builder = new ContainerBuilder(); GlobalConfiguration.Configuration.ServiceResolver.SetResolver( new AutofacWebAPIDependencyResolver(RegisterServices(builder)) ); } private static IContainer RegisterServices(ContainerBuilder builder) { builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).PropertiesAutowired(); builder.RegisterType<WordRepository>().As<IWordRepository>(); builder.RegisterType<MeaningRepository>().As<IMeaningRepository>(); return builder.Build(); } } 

Then initialize it in Application_Start :

 protected void Application_Start() { //... AutofacWebAPI.Initialize(); //... } 

Hope this helps.

+4
Feb 27 '12 at 8:38
source share



All Articles