AUTOFAC Official document (15) "Lifecycle Events"

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.