Helpers in MonoRail provide a very convenient way to extend your presentation logic. However, it’s important to note that MonoRail will initialize helper instances for a controller before the session state has been created for the request. The implication of not having session state when helpers are initialized is that you cannot check session state in a helper’s constructor. Consider the following:

[Helper(typeof(SomeHelper))]
public class SomeController : Controller
{
       public void SomeAction() {}
}

public class SomeHelper: AbstractHelper
    {
        public SomeHelper()
        {
            if (HttpContext.Current.Session["SOME_KEY"] != null)
                doSomething();
        }
}

When the request for /SomeController/SomeAction.rails is processed, the SomeHelper instance will be created. When the constructor code executes, a null reference exception will occur when attempting to access an item in session. The obvious implication is that any initialization code that is session state dependent cannot be in the constructor. The solution I’ve used is to create an initialize method that is called from any method requiring the setup routine.

Update 2008-02-21 - this issue cropped up with controllers too. Session is not created at the time controllers are instantiated either. Avoid constructors that check session state.