Copied to clipboard

Flag this post as spam?

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


  • Leon 101 posts 489 karma points
    May 26, 2016 @ 15:15
    Leon
    0

    OnApplicationStarted wont fire

    Why can't I get OnApplicationStarted to fire? Ultimately I want to use it to set up IoC. I believe I've followed the documentation correctly?

    Here is my code in Global.asax.cs:

    namespace UmbracoWebApi
    {
        //public class MvcApplication : System.Web.HttpApplication
        public class MvcApplication : Umbraco.Web.UmbracoApplication
        {
    
            protected override void OnApplicationStarted(object sender, EventArgs e)
            {
                string x = "y"; //This breakpoint doesn't get hit
            }
    
        }
    }
    

    Please help.

  • Leon 101 posts 489 karma points
    May 26, 2016 @ 15:49
    Leon
    100

    Ha, I found the answer based on this article: I basically needed to enable the global.asax in umbraco.

    http://jondjones.com/how-to-enable-the-global-asax-in-umbraco/

    As well as the changes to the codebehind as in the code already changed in the question of the post, I also needed to change the following:

    In the global.asax (not the codebehind), I changed this line:

    <%@ Application Inherits="Umbraco.Web.UmbracoApplication" Language="C#"%>
    

    To this:

    <%@ Application Codebehind="Global.asax.cs" Inherits="UmbracoWebApi.MvcApplication(This is the name of my application)" Language="C#" %>
    

    It's worth noting that umbraco has different overload which you will need to override to get the startup kind of stuff to work.

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    May 26, 2016 @ 16:44
    Nicholas Westby
    3

    I would actually recommend this alternative, which does not require you to modify the global.asax:

    namespace MyProject.App.EventHandlers
    {
    
        // Namespaces.
        using Umbraco.Core;
    
    
        /// <summary>
        /// Handles application startup.
        /// </summary>
        public class MyStartupHandler : ApplicationEventHandler
        {
    
            #region Methods
    
            /// <summary>
            /// Application starting.
            /// </summary>
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication,
                ApplicationContext applicationContext)
            {
    
                //TODO: Do some stuff.
    
    
                // Boilerplate.
                base.ApplicationStarting(umbracoApplication, applicationContext);
    
            }
    
    
            /// <summary>
            /// Application started.
            /// </summary>
            protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,
                ApplicationContext applicationContext)
            {
    
                //TODO: Do some stuff.
    
    
                // Boilerplate.
                base.ApplicationStarted(umbracoApplication, applicationContext);
    
            }
    
            #endregion
    
        }
    
    }
    

    You can read more about this here: https://our.umbraco.org/Documentation/Reference/Events/Application-Startup

  • Leon 101 posts 489 karma points
    May 27, 2016 @ 09:07
    Leon
    0

    Yay! Thanks Nicholas. That worked.

    At first, it wasn't clear to me that creating a class like this would just automatically / magically get called on startup, but it does (something to do with the inherited class).

    Leon

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    May 27, 2016 @ 17:12
    Nicholas Westby
    0

    Yep, Umbraco uses reflection to find classes that inherit from the base class so that it can instantiate them and call the event methods on them.

Please Sign in or register to post replies

Write your reply to:

Draft