User_start app code for umbraco website

I read that for umbraco to run my code when the application starts, I need to inherit from umbraco.Global and override Application_Start. I did this with the following simple code, which is in its own assembly, referenced by the umbraco website project, and it has a bin folder in it.

public class AtomicF1Global : umbraco.Global { protected override void Application_Start(object sender, EventArgs e) { base.Application_Start(sender, e); new WindsorStarter().Start(); throw new Exception("Reached Custom Global"); } } 

An exception is there to prove that it was not called.

As far as I know, all I have to do is what I did. I do not need to update the umbraco table anywhere (as happens when creating a number of different modifications for umbraco).

However, my code is never called, and I could not figure out why. Do I need to register somewhere?

I also checked that "App_Global.asax.dll" was not present in the bin directory.

I also tried creating Global.asax in the umbraco site project, which looks like this:

 <%@ Application Language="C#" Inherits="umbraco.Global" %> <%@ Import Namespace="atomicf1.domain" %> <script runat="server"> void Application_Start(object sender, EventArgs e) { // Call Global base class first base.Application_Start(sender, e); // Code that runs on application startup new WindsorStarter().Start(); throw new Exception("Reached Custom Global"); } </script> 

Umbraco version is 4.7.1 (.NET 4.0).

+7
source share
5 answers

I created an empty MVC solution in Visual Studio and added Umbraco 6.x. Edited global.asax.cs and has the same problem as you: the function does not work.

The solution seems simple, but a bit complicated:

In the global.asax file (if you double-click it, visual studio will open global.asax.cs, press "F7" or right-click global.asax and select "View Markup"), you need to change the "Inheritance" ". See the original global.asax file from umbraco:

 <% Application Codebehind="Global.asax.cs" Inherits="Umbraco.Web.UmbracoApplication" Language="C#" %> 

Change it like this:

 <%@ Application Codebehind="Global.asax.cs" Inherits="YourGlobalClassName" Language="C#" %> 

Note. If the name of your GlobalClassName is in the namespace, be sure to include the namespace, for example:

 <%@ Application Codebehind="Global.asax.cs" Inherits="YourNameSpace.YourGlobalClassName" Language="C#" %> 

See also:

Original controller documentation from umbraco.

A similar question (addressed to umbraco 6): stack overflow

+4
source

Implementing your own global.asax is the right way, as it will start when the application starts.

I implemented my global.asax file just like yours, however I put the code in a separate CS file in a separate project, but then this is only my way.

 public class Global : umbraco.Global { protected override void Application_Start(object sender, System.EventArgs e) { base.Application_Start(sender, e); XmlConfigurator.Configure(); ILog log = LogManager.GetLogger(typeof (Global)); log.Debug("Application started"); } } 

Considering the differences, you need to add protected override to your method.

Personally, I always add a log (using log4net) to my global.cs file so that I can verify that my log is running and that the application has rebooted.

If your code does not fit, you can try to make changes to the web.config file to force the application to restart, as well as run a debugger to make sure that your code just hasn’t been reached for any other reason.

+2
source

I would use the WebActivator package. You can then make your code run without worrying about global.asax and overriding Umbraco's methods.

For a quick start, see this blog post from Bart Wullems .

+1
source

I appreciate that this is a very old question, but this is one of the few options that I found when searching for a similar problem.

The solution that I defined is different from the ones mentioned in this article, so I thought that I was proposing this too.

I went ahead and wrote a CustomWebModule, which may be between the web request and any requests included in umbraco.

Step One was to create a custom implementation of IHttpModule

 namespace My.Application.Namespace { public class CustomWebModule : IHttpModule { static void AuthenticateRequest(object sender, EventArgs e){ // DO ALL FANCY STUFF in my app and then make calls to the // KEY umbraco functions from umbraco.core.security.AuthenticationExtensions // "AuthenticateCurrentRequest" function. // // specifically: setting the GenericPrincipal and related entities. } public void Dispose() { //throw new NotImplementedException(); // make sure you don't write a memory leak! // dispose of everything that needs it! } public void Init(HttpApplication app) { app.AuthenticateRequest += AuthenticateRequest; } } } 

note that the queries required in the Fancy Things section are listed in the Umbraco base code base:

https://github.com/umbraco/Umbraco-CMS/blob/dev-v7/src/Umbraco.Core/Security/AuthenticationExtensions.cs

then add the keys to the web.config file

 <system.web> <httpModules> <add name="CustomWebModule" type="My.Application.Namespace.CustomWebModule"/> 

and

 <system.webServer> <modules runAllManagedModulesForAllRequests="true"> <remove name="CustomWebModule"/> <add name="CustomWebModule" type="My.Application.Namespace.CustomWebModule"/> 
+1
source

If you want the code to run when the application starts, the way you usually do this is to create a class that inherits from ApplicationBase. Public classes that inherit from this are called when the Umbraco application starts. Here is a sample code from one of the launch events that we use on one of our Umbraco sites:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using umbraco.BusinessLogic; using umbraco.cms.businesslogic.web; using umbraco.BasePages; namespace MySite.Events { public class EventHandlers : ApplicationBase { public EventHandlers() { //put your code to run on app startup here } } } 
0
source

All Articles