This content is archived. See latest version here.

Last updated: Jul 09 2014

This document describes how to create an initialization module to work with the initialization system in the EPiServer platform.

When creating your own initialization module you need to add a reference to EPiServer.Framework.dll. The following Initialization code example shows how a property is set up with a default implementation in Initialize and then the process is undone in Uninitialize:

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

Recommendations

  1. Allow for Initialize to be called multiple times. If you carry out multi-step initialization in your Initialize method and if it is re-executed because of an exception – ensure that the application handles this scenario correctly. Example:
    C#
    private bool _eventAttached;
        public void Initialize(InitializationEngine context)
        {
              if (!_eventAttached)
           {
                 SomeClass.AnEvent += MyEventHandler;
                  _eventAttached = true;
           }
                    MethodThatMayThrowException();
        }
    This Initialize method may throw an exception after the event handler has been hooked up. The initialization system will re-invoke the Initialize method on the next request that reaches the web application and if the event hook-up is not protected with a flag it would get added again.
  2. The initialization engine will make sure that your code executes in a single-threaded manner. It is not necessary to lock regions when dealing with shared state etc. Note that this guarantee is only made for Initialize and Unintialize when executing through the initialization system. If you have custom code that makes any calls directly into your initialization module then you may need to deal with multi-threading issues.
  3. Remember that the initialization system tracks the initialization state of your module.
  4. Do an implementation of Uninitialize. Anything done by Initialize should be undone by Uninitialize. It should also be undone in the reverse order of Initialize.
  5. Do not add logic to the Preload method. If you select the option in Visual Studio to implement the IInitializableModule interface the Preload method will be generated as “throw new NotImplementedException();”, remember to remove this code. The Preload method has been added to support the “Always running” concept in ASP.NET 4 but is currently not being called by EPiServer.

See also

Comments