Tuesday, March 31, 2015

EPi Initialization Modules: A Practical Guide

If you've been working with EPi 7 or 7.5 (or even 8 for you early adopters), you've most likely created an initialization module.  For a thorough introduction, this link is a good resource. While they are really extensible, I've run into a few hiccups during development and would like to share some (hopefully) time saving tips.

Debugging

Debugging has proven to be quite a headache for me whenever I create an initialization module since I don't do it very often and always forget how I was able to do it the last time. Since you need to be attached to the debugger while the site is initializing, many common forms of debugging don't work very well (if you are using traditional IIS and not IIS Express). I've seen a couple blog posts on this topic, but none have worked for me (because it's for an older version of EPi or because it recycles the whole app pool).  

Here is the easiest way I've found to quickly and easily debug my initialization modules:

1. In your Initialization module, add this line where you would like the debugger to break

System.Diagnostics.Debugger.Break();

2. Using Visual Studio's Debug > Attach to Process menu, attach to all of the w3wp processes.  This makes sure that you are still attached to the process running the initialization even when you restart the site in the next step.



3. In IIS, restart the website you'd like to initialize

You should see your break point hit and be able to debug your initialization module now.  If you know a better way to accomplish this, please let me know in the comments! This has been the simplest solution I've found.

InitComplete

In EPi's documentation, they mention that the InitComplete event's "typical use case (also featured in EPiServer Community) is to attach event handlers to an instance property that may be overridden by third-party code."

Another use case for InitComplete is if you need to access the Start page of your site or any other site specific properties.  These are only available after the site has been initialized.

One "gotcha" to keep in mind, is that you must have the wild card site binding in order for the initialization module to properly find your Start Page.  Otherwise, ContentReference.StartPage returns an empty content reference.



Do you know of any other tips when developing initialization modules? Let me know in the comments!