I heard good things about Softsys Hosting and so I decided to move my ASP.NET MVC solution over to them. But it would not run on them. I was able to pinpoint the problem to my BeginRequest event handlers. If I had them I'd get an error. Here is my code.
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
this.EndRequest += new EventHandler(MvcApplication_EndRequest);
}
void MvcApplication_EndRequest(object sender, EventArgs e)
{
}
void MvcApplication_BeginRequest(object sender, EventArgs e)
{
}
I could reproduce the problem by just creating the default ASP.NET MVC application and adding the above code. The strange thing is this code worked fine on my old host and it only crashes on my new (shared) host. If I have these event handlers in my code I get this error:
Server Error in '/' Application. Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.] System.Web.PipelineModuleStepContainer.GetStepArray(RequestNotification notification, Boolean isPostEvent) +27 System.Web.PipelineModuleStepContainer.GetEventCount(RequestNotification notification, Boolean isPostEvent) +11 System.Web.PipelineStepManager.ResumeSteps(Exception error) +205 System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +91 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +514
I tried troubleshooting this with Softsys, but they were not very helpful, basically they just confirmed that I had turned on the "ASP.NET Pipeline (MVC)" functionality within my admin control panel.
Can someone:
You need register your handlers in each HttpApplication instance. There may be several pooled instances of HttpApplication. Application_Start is called only once (for IIS 6 and IIS 7 in classic mode - on the first request, for IIS 7 integrated mode - on web app start, just before any request). So to get all working you need to add events handlers in overrided Init method of HttpApplication or in constructor of it. If you add them in constructor - these handlers will be invoked first, even before the handlers of registered modules.
So your code should look like this:
public class MySmartApp: HttpApplication{
public override void Init(){
this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
this.EndRequest += new EventHandler(MvcApplication_EndRequest);
}
protected void Application_Start(){
RegisterRoutes(RouteTable.Routes);
}
}
or like this:
public class MySmartApp: HttpApplication{
public MySmartApp(){
this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
this.EndRequest += new EventHandler(MvcApplication_EndRequest);
}
protected void Application_Start(){
RegisterRoutes(RouteTable.Routes);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With