AUTOFAC exposes events that may be hooked at different stages of the instance lifecycle. These are subscribed during component registration (or by attaching to the Icomponentregistration interface). )
Onactivating
onactivated
onrelease
onactivating
The Onactivating event is raised before the component is used. Here you can: switch the instance to another or wrap it in an agent to do property injection or method injection to perform other initialization tasks
In some cases, such as registertype<t> (), the registered specific type is used for type resolution and is used by Activatingeventargs. For example, the following will fail, and a class conversion exception occurs:
Builder. Registertype<tconcrete> ()//failure: A class of binterfacesubclass types will be thrown
. As<tinterface> () //Type Tconcrete
. Onactivating (e => e.replaceinstance (New Tinterfacesubclass ()));
One simple workaround is to sign up in two steps:
Builder. Registertype<tconcrete> (). Asself ();
Builder. Register<tinterface> (c => c.resolve<tconcrete> ())
. Onactivating (e => e.replaceinstance (New Tinterfacesubclass ()));
onactivated
Once the component is fully built, the Onactivated event is raised. Here, you can perform application-level tasks that depend on fully built components-these should be few. Onrelease
The Onrelease event replaces the standard Cleanup behavior of the component. The standard cleanup behavior for components that implement IDisposable and that are not marked as externallyowned () is to invoke the Dispose () method. Standard Cleanup behavior for components that do not perform IDisposable or are marked externally owned is not operational-do nothing. Onrelease overrides this behavior with the provided implementation.