Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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.
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.
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
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
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.
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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:
Please help.
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:
To this:
It's worth noting that umbraco has different overload which you will need to override to get the startup kind of stuff to work.
I would actually recommend this alternative, which does not require you to modify the global.asax:
You can read more about this here: https://our.umbraco.org/Documentation/Reference/Events/Application-Startup
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
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.
is working on a reply...