EPiServer - initialization modules

What is the initialization module?

Initialization module is a great alternative to Application_Start method in Global.asax for running custom code on application startup.

Let's say you're building a new addon or plugin that should perform some logic every time the page is published.

First, you need to attach to "page published" event and then execute the custom logic.

To attach to "page published" event you can, of course, use Application_Start method in Global.asax.cs file, but what if you want to distribute the code as a NuGet package, and you don't have access to Global.asax.cs?

Initialization module comes to the rescue!

To create an initialization module, you need to create a class that implements IInitializableModule interface, and decorate it with InitializableModule attribute:

[InitializableModule]
public class SampleInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
    }
	
    public void Uninitialize(InitializationEngine context)
    {
    }
	
    public void Preload(string[] parameters)
    {
    }
}

In Initialize method, you can attach to various events and write the code that will be executed on application startup.

Everything that was initialized in Initialize method should be uninitialized in Uninitialize method. We don't want memory leaks, don't we?

Preload method, according to documentation, has been added to support the "always running" concept in ASP.NET 4, but it's not called by EPiServer, and therefore should be empty.

So here is the sample code:

[InitializableModule]
public class MyInitializationModule : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        // attach to PublishContent event
        ServiceLocator.Current.GetInstance<IContentEvents>().PublishedContent += OnPublishedContent;
    }

    private void OnPublishedContent(object sender, EPiServer.ContentEventArgs e)
    {
        // Content can be anything: page, block, file, etc.
        // We are only interested in pages
        var page = e.Content as PageData;
        if (page == null)
        {
            return;
        }

        // my custom logic...
    }

    public void Uninitialize(InitializationEngine context)
    {
        // dettach from PublishContent event
        ServiceLocator.Current.GetInstance<IContentEvents>().PublishedContent -= OnPublishedContent;
    }

    public void Preload(string[] parameters)
    {
    }
}

You can place this file anywhere in your solution, set a breakpoint in the OnPublishedContent method, and see what happens when you publish a page, a block or a file in the edit mode.

How does this work?

On application startup, more specifically in EPiServer.Global, the base class of Global.asax.cs, the ASP.NET will trigger the EPiServer's initialization engine which scans all assemblies to find classes that implement IInitializableModule interface.

It will then check if those classes are decorated with InitializableModule or ModuleDependency attribute (you don't need both) and execute them one by one.

Initialization engine can be found in EPiServer.Framework.Initialization namespace.

comments powered by Disqus