HomeDev GuideRecipesAPI Reference
Dev GuideAPI ReferenceUser GuideLegal TermsGitHubNuGetDev CommunityOptimizely AcademySubmit a ticketLog In
Dev Guide

Create an initialization module

Describes how to create an initialization module to work with the initialization system in Optimizely Content Management System (CMS 10 and 11).

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

[InitializableModule]
public class SampleInitialization: IInitializableModule {
  public void Initialize(InitializationEngine context) {}
  public void Uninitialize(InitializationEngine context) {}
}
  • 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 error – ensure that the application handles this scenario correctly. Example:
    private bool _eventAttached;
    public void Initialize(InitializationEngine context) {
      if (!_eventAttached) {
        SomeClass.AnEvent += MyEventHandler;
        _eventAttached = true;
      }
      MethodThatMayThrowException();
    }
    
  • This Initialize method may generate an error after the event handler is hooked up. The initialization system invokes the Initialize method again on the next request that reaches the web application, and if the event hook-up is not protected with a flag, it gets added again.
  • The initialization engine makes sure that your code executes in a single-threaded manner. You do not need to lock regions when you deal with a shared state. This guarantee applies to Initialize and Unintialize when you execute through the initialization system. If you have custom code that makes calls directly into your initialization module, you may need to deal with multi-threading issues.
  • Remember that the initialization system tracks the initialization state of your module.
  • Implement Uninitialize. Anything you do with Initialize you should undo with Uninitialize in the reverse order of Initialize.
  • If you are using the CMS API, make sure you are adding a module dependency to CMS using [ModuleDependency(typeof(EPiServer.Web.InitializationModule))].