Attach EPiServer event handlers on startup using InitializableModule

Denna artikel har migrerats från en tidigare version av vår webbplats och kan därför avvika i utseende och funktionalitet.

Den här artikeln är inte översatt till svenska och visas därför på engelska istället.


Explanation of the new InitializableModule concept in EPiServer 6 which is used to implement functionality that should execute when an EPiServer site starts, for example to attach event handlers.

Uppskattad lästid : 3 minuter

Gå till avsnitt

New initialization process in EPiServer 6

In EPiServer 5 we commonly used the PlugInAttribute attribute class to execute code when an EPiServer site starts, for example to attach event handlers.

In EPiServer 6 this can be done by implementing the IInitializableModule interface and decorating the class with the InitializableModule attribute.

An InitializableModule class will be executed on startup, invoking its Initialize method. The order in which these modules are initialized is determined by any dependencies existing between different modules. But EPiServer takes care of all that, as long as you specify which modules (if any) your module depends on.

Create a new InitializableModule class to attach event handlers

Here’s a sample InitializableModule class which attaches an event handler to the CreatedPage event. Note that a dependency has been specified using the ModuleDependency attribute which accepts a single type, or an array of types, which specify which other InitializableModule types must be initialized before the current module. This attribute is optional, but you must set the InitializableModule attribute in addition to implementing the IInitializableModule interface:

[InitializableModule]
[ModuleDependency(typeof(MyOtherStartupModule))]
public class MyStartupModule : IInitializableModule
{
    private bool _eventsAttached = false;
 
    #region IInitializableModule Members
 
    public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
    {
        // Attach event handlers unless they've already been attached
        // The initialize method may be executed repeatedly if an exception
        // is thrown, but we don't want additional event handlers to attach
        // if the exception is thrown later in the call stack
        if(!_eventsAttached)
        {
            // Attach event handler to when a page has been created
            DataFactory.Instance.CreatedPage+=CreatedPageHandler;
 
            _eventsAttached = true;
        }
    }
 
    public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context)
    {
        // Detach event handlers
        DataFactory.Instance.CreatedPage -= CreatedPageHandler;
    }
 
    public void Preload(string[] parameters)
    {
        throw new NotImplementedException("No preload required");
    }
 
    #endregion
 
    void CreatedPageHandler(object sender, PageEventArgs e)
    {
        // Do something with the new page
        DoSomething(e.Page);
    }
}