2

パブリック ILogger Log プロパティが自動的に設定されるように、Autofac 2.5 でプロパティ インジェクションを有効にするにはどうすればよいですか?

次の方法を使用して、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 では機能しないようです。

4

1 に答える 1

2

これで、登録のメソッドを使用してPropertiesAutowired、プロパティの挿入を実行する必要があることを示すことができます。

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

特定のアセンブリ内のすべてのオブジェクトにこれを設定するには、AutofacsRegisterAssemblyTypesでPropertiesAutowiredを使用できます。

var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(typeof(Foo).Assembly)
    .PropertiesAutowired();
于 2012-04-09T11:57:11.150 に答える