Todd Smith
Todd Smith

Reputation: 17272

Property Injection with Autofac 2.5

How do I enable property injection in Autofac 2.5 so that my public ILogger Log property gets set automagically?

I was using the following method to enable property injection in a MVC3 project with Autofac 2.4

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> ();

but this no longer seems to work with Autofac 2.5.

Upvotes: 2

Views: 696

Answers (1)

Alex Meyer-Gleaves
Alex Meyer-Gleaves

Reputation: 3831

You can now use the PropertiesAutowired method on a registration to indicate that property injection should be performed.

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

To set this for all objects in a particular assembly you can use PropertiesAutowired with Autofacs RegisterAssemblyTypes:

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

Upvotes: 2

Related Questions