【问题标题】:Property Injection with Autofac 2.5Autofac 2.5 的属性注入
【发布时间】:2012-10-25 10:05:08
【问题描述】:

如何在 Autofac 2.5 中启用属性注入,以便自动设置我的公共 ILogger Log 属性?

我正在使用以下方法在 Autofac 2.4 的 MVC3 项目中启用属性注入

public class InjectPropertiesByDefaultModule : Autofac.Module
{
    protected override void AttachToComponentRegistration (IComponentRegistry componentRegistry, IComponentRegistration registration)
    {
        registration.Activating += (s, e) =>
        {
            e.Context.InjectProperties (e.Instance);
        };
    }
}


builder.RegisterModule<InjectPropertiesByDefaultModule> ();

但这似乎不再适用于 Autofac 2.5。

【问题讨论】:

    标签: asp.net-mvc-3 autofac


    【解决方案1】:

    您现在可以在注册上使用PropertiesAutowired 方法来指示应该执行属性注入。

    var builder = new ContainerBuilder();
    builder.RegisterType<Foo>().PropertiesAutowired();
    

    要为特定程序集中的所有对象设置此项,您可以将 PropertiesAutowired 与 Autofacs RegisterAssemblyTypes 一起使用:

    var builder = new ContainerBuilder();
    builder.RegisterAssemblyTypes(typeof(Foo).Assembly)
        .PropertiesAutowired();
    

    【讨论】:

    • 有没有办法自动设置 PropertiesAutowired 而不必为每个注册的对象指定它?
    • 托德,我也找不到办法做到这一点。我评论了文档;也许尼古拉斯在那里回答:code.google.com/p/autofac/wiki/PropertyInjection
    猜你喜欢
    • 2011-12-01
    • 2017-01-26
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多