This can be done with a module, e.g.
class InjectPropertiesByDefaultModule : Autofac.Module {
protected override void AttachToComponentRegistration(
IComponentRegistration registration,
IComponentRegistry registry) {
registration.Activating += (s, e) => {
e.Context.InjectProperties(e.Instance);
};
}
}
Then:
builder.RegisterModule<InjectPropertiesByDefaultModule>();
I think you might be misunderstanding the true
paramerter to PropertiesAutowired
- it determines how circular dependencies are supported and should probably remain false
. To emulate the true
setting you can attach to Activated
instead of Activating
above.
However, if at all possible, use constructor injection even for "optional" dependencies like your ILog
. It leads to cleaner components (e.g. fields can be made readonly
) and dependencies are more understandable (they're all in the constructor, and there's no guessing about the meaning of individual properties.)
Only consider using property injection when there are multiple configurations of the application and in some configurations the dependency will be truly absent.
Even in these cases, the "Null Object" pattern is usually a better fit.