Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofac - Inject properties into a asp.net mvc controller

I have a base controller from which inherit all my controllers. This base controller has some properties I'd like to inject the using property injection.

My controller registration looks like this

builder.RegisterControllers(Assembly.GetExecutingAssembly()

I don't know how to access the base class and inject the properties.

like image 499
user256034 Avatar asked May 05 '11 08:05

user256034


People also ask

How Autofac is implemented in MVC?

To get Autofac integrated with MVC you need to reference this MVC integration NuGet package, register your controllers, and set the dependency resolver. You can optionally enable other features as well.

Can we inject the dependency to individual action method of the controller?

You can take advantage of the ServiceFilter attribute to inject dependencies in your controller or your controller's action methods.

How can we inject the service dependency into the controller?

ASP.NET Core injects objects of dependency classes through constructor or method by using built-in IoC container. The built-in container is represented by IServiceProvider implementation that supports constructor injection by default.


1 Answers

This should work:

builder.RegisterControllers(typeof(MvcApplication).Assembly).PropertiesAutowired();

Some more info on the autofac website: http://code.google.com/p/autofac/wiki/PropertyInjection

like image 96
nickvane Avatar answered Oct 04 '22 22:10

nickvane