属性注入因Autofac失败 [英] Property Injection fails with Autofac
问题描述
我正在将Autofac与MVC/Owin和WebApi一起使用.
I am using Autofac with MVC / Owin and WebApi.
使用Autofac文档后,我正在使用设置:
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;
}
}
因此_testConstructor被注入,并且TestProperty始终为null.
So _testConstructor is injected and TestProperty is always null.
我什至在Index方法中检查了它的值,并且它为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 ...
有人可以帮我吗?
更新1
添加.PropertiesAutowired();到Controller的RegisterController工作,但不适用于ViewPages.
Adding .PropertiesAutowired(); to RegisterController work for controllers but not for ViewPages.
我正在如下使用ViewPageBase:
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
然后在Autofac设置中,我有:
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();
但是当我在视图中访问测试"属性时,该属性为null.
But when I access Test properties in my views it is null.
为什么?
更新2
如果在我的布局视图中添加:
If in my layout view I add:
@{
var test = DependencyResolver.Current.GetService<ITest>();
}
测试已正确解决...
test is resolved correctly ...
布局页面和Autofac可能是一个问题吗?
Maybe is this a problem with Layout Pages and Autofac?
更新3
我能够复制问题并在 https://github.com/mdmoura/MvcAutofac
如果您运行项目,则第二行代码的_Layout主页上将出现错误:
If you run the project there will be an error on _Layout master page on the second code line:
@SettingsA.Get()
@SettingsB.Get()
SettingsA使用DependencyResolver在ViewPagePage中解析,并且可以正常工作.
SettingsA is resolved in ViewPagePage using DependencyResolver and it works.
通过SettingsB,我正在尝试使用属性注入,但是没有运气.
With SettingsB i am trying to use Property Injection and no luck.
Autofac配置位于global.asax Application_Start中.
Autofac configuration is in global.asax Application_Start.
有人知道怎么了吗?
推荐答案
在 Autofac 中不会自动完成属性注入.您必须告诉 Autofac 在您的控制器注册上注入属性.
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.
这篇关于属性注入因Autofac失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!