How can I register global callback on Autofac container which is triggered whenever any object is resolved?
I want to use reflection and check if an object has a method called Initialize() and call it if it does. I want it to be duck typed i.e. no interfaces are required.
Thanks!
In Autofac you can use the
IComponentRegistrationinterface to subscribe on various lifetime events:You can get the
IComponentRegistrationinstance by creating aModuleand override theAttachToComponentRegistrationmethod:Now you only need to register your module in your container builder:
and the
OnActivatedmethod will be called after every component activation no mater in which module you have registered the component.