Initialization Modules Explained

On every project, you’ll need certain configuration code to run when your website is loaded for the first time.  In EpiWorld, this is done by something called an InitializationModule.  Before we delve into how initialization modules work, we should probably talk about what sort of things you will be using them for.

What things will I put in my initialisation module?

So, for starters, Episerver uses a structure map to do it’s dependency injection.  If you want to write unit tests for your code, then you will need a place to define your mappings.  On a project, I will normally create a class called StructureMapSetUp.  In here I will define all my interface DI configurations.

A custom view engine.  If you want to structure your project a bit more nicely and you don’t want to just dump all your view in the View folder, then you’ll want to create a custom view engine.  For example, I would usually add a Pages and Block sub folders in your View folder to make it easier to find the view you want.  Overriding the default is the sort of thing an initialization module is used for.

GlobalFiltersSetup.  This will add any GlobalFilters you want in your project.  The main one I use is to register a custom PageContextActionFilter.  A PageContextActionFilter is a useful way to intercept the call from the controller to the view.  This allows us to get and inject all the page settings like header, footer, breadcrumb settings in a global way into the base controller, so all the background things are taken care of outside of each individual controller.

I hope that gives you a small flavour of some of the things you’ll need to add into your project, luckily, setting up a is very easy.

The Initialization Module Code

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class InitializationModule : IInitializableModule
{
internal Injected<IEpiserverDependencies> EpiserverDependencies;
public void Initialize(InitializationEngine context)
{
context.InitComplete += InitCompleteHandler;
}
public void Preload(string[] parameters)
{
}
public void Uninitialize(InitializationEngine context)
{
}
private void InitCompleteHandler(object sender, EventArgs e)
{
}
}

An InitializableModule class will be executed on application start-up.  You will need to add your code into the Initialize method which is automatically invoked when the module is executed.

To create an InitializableModule, the first thing you need to do is decorate your class with a [InitializableModule] attribute.  You will also need a [ModuleDependency].  The ModuleDependency attribute tells Episerver what to run before the code is executed.  You will also need to add a reference to EPiServer.Web.InitializationModule.  If you have code that replies on your structure map dependencies, you may also need to put in a refernce to your StructureMapSetUp configuration class.

You may be asking where you need to add your Initializable Module?  I usually put mine in an App_Start folder.  The good thing about this is, it’s run at runtime.  The bad thing is visual studio doesn’t always pick up your coding mistakes when you build the project, so you end up loading the page and finding out something isn’t right.  A common one is the structure map configuration, if you have code in App_Start and you change a constructor and build, the code in the App_Start won’t  complain until you run the site. It’s easy to fix but is a bit annoying.

Conclusion

We have gone over how to get code to run when your website is first loaded and the type of things you’ll want to use this handy feature for.  Creating an InitializableModule is very simple and quick to get started with.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

4 replies

Trackbacks & Pingbacks

  1. […] is how to switch the background images. In order to change the screens, we will need to create an initialization modules. In the initialization modules, we will use a VirtualPathMappedProvider to intercept the login […]

  2. […] module. If you have never come across one of these before then I would recommend that you read this article […]

  3. […] module. If you have never come across one of these before then I would recommend that you read this article […]

  4. […] To trigger the partial router we need to create an Initialization Module. If you haven’t come across one of these yet, I would recommend reading Initialization Modules Explained. […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *