In this tutorial, you will learn about Episerver initialisation modules, what they are and how you can create one. On pretty much every Episerver project that you will work on, you will want to customise how the CMS or your application works. For example, you might want to do some IoC configuration, add some custom routing, or even warm up a cache. These types of tasks are done by hooking into the .NET pipeline and running some custom code. In Episerver, the way to add code and make it run on application start-up is done by creating something called an Initialization module. Luckily, Initialization modules are easy to implement, read on to learn how 🔥🔥🔥

The Initialization Module Code

Let us start things off by looking at the code:

To create a new initialisation module, create a new class and decorate it with the [InitializableModule] attribute. Simply doing this will mean the code is executed on application start-up. Next, you need to make sure the class implements from IInitializableModule. Within Initialize() you can hook into an event. On Line17, InitCompleteHandler will be called when ready. You can add whatever custom code you want to in here!

Ordering

Sometimes when you create lots of initialization modules, you might care about the order in which they execute. For example, you might want your structure map dependencies to have been configured before a certain module is run. This is simple in Episerver as you can order initialization modules using the ModuleDependency attribute:

The ModuleDependency attribute tells Episerver what to run before the module is executed. If you have code that relies on your structure map dependencies, you may also need to put in a reference to your StructureMapSetUp configuration class. Be careful to not accidentally create an infinite loop by chaining two modules in a circular reference though ❌

Ideas For Initialisation Modules

Dependency Injection: The most common reason to build an initialisation module on a project is to configure the IoC container and deal with dependency management. Episerver ships with Structure map to do its dependency injection. On a project, I will normally create an initialisation module called StructureMapSetUp. This is where I define all the project DI configurations.

Custom View Engine: If you want to have a well-organised view folder with easy to find views, you will want to create a custom view engine. I usually create two folders in my Views folder, one called Pages and one called Blocks. By default, this will work so instead of having to update all the code in every controller, you can create a custom view engine that looks in these new locations.

Global Filters: Global filters can be useful to intercept the call from the controller to the view. Creating a filter that gets called before any controller will allow you to inject data into the page request, like all the website settings, header data, and footer data. using this technique means you can get access to data in all controllers easily!


If you need some code to run when your website starts, you will need to create an initialization module.  As you have seen, creating an InitializableModule is very simple. All you need to do is implement the correct attributes and interfaces and off you go! Happy Coding 🤘