In Autofac, you can use the interface IComponentRegistrationto subscribe to various life events:
- Onactivating
- Onactivated
- Onrelease
You can get an instance IComponentRegistrationby creating Moduleand overriding the method AttachToComponentRegistration:
public class EventModule : Module
{
protected override void AttachToComponentRegistration(
IComponentRegistry componentRegistry,
IComponentRegistration registration)
{
registration.Activated += OnActivated;
}
private void OnActivated(object sender, ActivatedEventArgs<object> e)
{
e.Instance.GetType().GetMethod("Initialize").Invoke(e.Instance, null);
}
}
:
var builder = new ContainerBuilder();
builder.RegisterModule<EventModule>();
OnActivated , , .