After all registrations, I am doing ContainerBuilder.RegisterCallback
and subscribing to all IComponentRegistration.Preparing
and IComponentRegistration.Activating
events to be able to handle all activations. With this two events I am able to build a tree, the order of events looks like this:
- Preparing: Root
- Preparing: FirstLevel_A
- Activating: FirstLevel_A
- Preparing: FirstLevel_B
- Preparing: SecondLevel_C
- Activating: SecondLevel_C
- Activating: FirstLevel_B
- Activating: Root
But what if some registrations are not Per Dependency
and I will have a graph instead of a tree. Is it possible to handle this case?
According to this answer there's another way of handling these events:
It's harder, but should do the job.
Not an answer, but it is too big for a comment.
AutoFac is a great IoC container, but it has 2 major problems. One is the awful registration API and the other one is a complete lack of diagnostics. The original author of AutoFac once tried to create an application that would help with this: Whitebox. The development has stopped and moved on to Autofac Analysis, that hasn't been active in years.
What you want to do requires a lot of insight into the inner workings of AutoFac, so you might want to check out the sources for ideas on how to accomplish what you need.