Property Injection fails with Autofac

给你一囗甜甜゛ 提交于 2019-12-10 18:18:03

问题


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?


回答1:


Properties injection is not done automatically in Autofac. You have to tell Autofac to inject properties on your controller registration.

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

See Property and Method Injection for more information on autowiring properties.



来源:https://stackoverflow.com/questions/29535893/property-injection-fails-with-autofac

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!