Observing BeginRequest (and friends) with IApplicationEventHandler in 6.0.5
Hi!
I'm trying to hook onto the BeginRequest event in Umbraco 6.0.5, but it crashes when I try to add the event handler to UmbracoApplicationBase.BeginRequst:
[NullReferenceException: Object reference not set to an instance of an object.] System.Web.PipelineStepManager.ResumeSteps(Exception error) +1239 System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +95 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +186
This is my code:
public class Foobar : IApplicationEventHandler { public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { LogHelper.Info<Foobar>("Initialized"); }
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { LogHelper.Info<Foobar>("Starting"); }
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) { LogHelper.Info<Foobar>("Started"); umbracoApplication.BeginRequest += (object sender, EventArgs e) => { }; }
}
Obviously I'm doing something wrong - any clues? :-)
Tried to do this with Global.asax.cs and Umbraco.Web.UmbracoApplication, but the result is the same. It seems the error is triggered when the handler is called.
Code:
public class UmbracoApplication : Umbraco.Web.UmbracoApplication
{
protected override void OnApplicationStarted(object sender, EventArgs e)
{
LogHelper.Info("Started");
base.OnApplicationStarted(sender, e);
// Same result without the EventHandler wrapper:
this.BeginRequest += new EventHandler(UmbracoApplication_BeginRequest);
LogHelper.Info("Event handler added");
}
private void UmbracoApplication_BeginRequest(object sender, EventArgs e)
{
}
}
Log:
2013-05-16 14:06:24,526 [10] INFO Umbraco.Site.UmbracoApplication - [Thread 76] Started
2013-05-16 14:06:24,526 [10] INFO Umbraco.Site.UmbracoApplication - [Thread 76] Event handler added
Error:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.PipelineStepManager.ResumeSteps(Exception error) +1239
System.Web.HttpApplication.BeginProcessRequestNotification(HttpContext context, AsyncCallback cb) +95
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +186
Found something that worked when extending Umbraco.Web.UmbracoApplication (but unfortunately not IApplicationEventHandler): Ignore the EventHandler properties and implement handler methods prefixed with "Application_", like so:
public void Application_BeginRequest(object sender, EventArgs e) { // ... }
Now if only I could figure out how to do that when implementing IApplicationEventHandler :-/
public class Foobar : IApplicationEventHandler {
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
LogHelper.Info<Foobar>("Initialized");
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
LogHelper.Info<Foobar>("Starting");
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) {
LogHelper.Info<Foobar>("Started");
// In ASP.NET there can by multiple instances of the HttpApplication.
// The ApplicationInit event will be raised on every HttpApplication.Init()
UmbracoApplicationBase.ApplicationInit += Init;
}
private void Init(object sender, EventArgs eventArgs) {
var app = (HttpApplication) sender;
app.BeginRequest += (s, e) => { };
}
}
Observing BeginRequest (and friends) with IApplicationEventHandler in 6.0.5
Hi!
I'm trying to hook onto the BeginRequest event in Umbraco 6.0.5, but it crashes when I try to add the event handler to UmbracoApplicationBase.BeginRequst:
This is my code:
Tried to do this with Global.asax.cs and Umbraco.Web.UmbracoApplication, but the result is the same. It seems the error is triggered when the handler is called.
Code:
Log:
Error:
Found something that worked when extending Umbraco.Web.UmbracoApplication (but unfortunately not IApplicationEventHandler): Ignore the EventHandler properties and implement handler methods prefixed with "Application_", like so:
Now if only I could figure out how to do that when implementing IApplicationEventHandler :-/
Could it be related to http://stackoverflow.com/questions/7005646/nullreferenceexception-pipelinestepmanager-resumesteps-at-godaddy ?
Anyone?
Thanks for sharing this Martin! I've been trying to tap into the
BeginRequest
event and my attempts were unsuccessful until I tried this.is working on a reply...