Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • HC Saustrup 28 posts 79 karma points
    May 16, 2013 @ 12:13
    HC Saustrup
    0

    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? :-)
  • HC Saustrup 28 posts 79 karma points
    May 16, 2013 @ 14:10
    HC Saustrup
    0

    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
    
  • HC Saustrup 28 posts 79 karma points
    May 16, 2013 @ 14:32
    HC Saustrup
    0

    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 :-/

  • HC Saustrup 28 posts 79 karma points
    May 16, 2013 @ 14:42
  • HC Saustrup 28 posts 79 karma points
    May 21, 2013 @ 10:16
    HC Saustrup
    0

    Anyone?

  • Martin Meixger 17 posts 75 karma points
    May 20, 2015 @ 11:30
    Martin Meixger
    2
    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) => { };
        }
    }
    
  • Simon Dingley 1474 posts 3451 karma points c-trib
    Oct 24, 2018 @ 12:32
    Simon Dingley
    0

    Thanks for sharing this Martin! I've been trying to tap into the BeginRequest event and my attempts were unsuccessful until I tried this.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies