I am using Autofac with MVC / Owin and WebApi.
Following Autofac documentation I am using the setup:
public static void Run(IAppBuilder application) {
ContainerBuilder builder = new ContainerBuilder();
HttpConfiguration configuration = GlobalConfiguration.Configuration;
builder.RegisterControllers(typeof(MvcApplication).Assembly);
builder.RegisterModelBinders(typeof(MvcApplication).Assembly);
builder.RegisterModelBinderProvider();
builder.RegisterModule<AutofacWebTypesModule>();
builder.RegisterSource(new ViewRegistrationSource());
builder.RegisterFilterProvider();
builder.RegisterApiControllers(typeof(MvcApplication).Assembly);
builder.RegisterWebApiFilterProvider(configuration);
builder.RegisterType<Test>().As<ITest>().PropertiesAutowired();
IContainer container = builder.Build();
application.UseAutofacMiddleware(container);
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
}
I then tested constructor and property injection on a controller:
public interface ITest { }
public class Test : ITest { }
public partial class HomeController : Controller {
private ITest _testConstructor { get; set; }
public ITest TestProperty { get; set; }
public HomeController(ITest testConstructor) {
_testConstructor = testConstructor;
}
public virtual ActionResult Index() {
var test = TestProperty;
}
}
So _testConstructor is injected and TestProperty is always null.
I even checked its value inside Index method and it is null ...
I tried different configurations and in different parts of the application and Property injection always fails ...
Can someone, please, help me out with this?
Update 1
Adding .PropertiesAutowired(); to RegisterController work for controllers but not for ViewPages.
I am using a ViewPageBase as follows:
public abstract class ViewPageBase : WebViewPage {
public ITest Test { get; set; }
} // ViewPageBase
public abstract class ViewPageBase<T> : WebViewPage<T> {
public ITest Test { get; set; }
} // ViewPageBase
And then in Autofac setup I have:
builder.RegisterSource(new ViewRegistrationSource());
builder.RegisterType<Test>().As<ITest>();
builder.RegisterType<WebViewPage>().PropertiesAutowired();
builder.RegisterGeneric(typeof(WebViewPage<>)).PropertiesAutowired();
But when I access Test properties in my views it is null.
Why?
Update 2
If in my layout view I add:
@{
var test = DependencyResolver.Current.GetService<ITest>();
}
test is resolved correctly ...
Maybe is this a problem with Layout Pages and Autofac?
Update 3
I was able to replicate the problem and created a project in https://github.com/mdmoura/MvcAutofac
If you run the project there will be an error on _Layout master page on the second code line:
@SettingsA.Get()
@SettingsB.Get()
SettingsA is resolved in ViewPagePage using DependencyResolver and it works.
With SettingsB i am trying to use Property Injection and no luck.
Autofac configuration is in global.asax Application_Start.
Does anyone knows what might be wrong?