Null reference exception from an initializationmodule that access UrlRewriteProvider when upgrading to R2

This is the first post in a small series about my experience in upgrading a somewhat large site from CMS 6 R1 to R2. I was expecting (or at least hoping) for it to be smooth sailing so I felt my hand slowly reaching for my palm when I was greeted with this

The error occurs in the step that upgrades xforms name, or “C:\Program Files (x86)\EPiServer\CMS\6.1.379.0\Upgrade\System Scripts\Upgrade XForm Names.ps1:36 char:22″ to be specific. The stack trace pointed to the following line in a initialization module

EPiServer.Global.UrlRewriteProvider.ConvertingToInternal += CancelRewriteIfDynamicData;

The “fix” for this was to simply do a null check in the module which got the installation to pass.

// TODO - remove this after upgrade is done
if (EPiServer.Global.UrlRewriteProvider == null)
  • Johan Björnfot

    This occurs because the XForm upgrade cmdLet needs an initialized EPiServer runtime. During this initialization all InitializableModules will be called on Initialize method as well.
    If your custom module requires some web context or you do not want it to execute outside the web (e.g. in installer context) then you can use the HostType flag on InitializationEngine to see which context the module executes in, like:

    public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context)
    if (context.HostType == Framework.Initialization.HostType.WebApplication)
    //This code will only run on site not in installer

    • Stefan Forsberg

      Thanks for the feedback Johan!