Copied to clipboard

Flag this post as spam?

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


  • Ricardo Gomes 22 posts 42 karma points
    Oct 19, 2009 @ 16:11
    Ricardo Gomes
    0

    Extending Global.asax

    hello all,

    I need to extend Application_Start, Application_End, Session_Start and Session_End methods from Global.asax.

    I have seen several posts regarding the extention these methods, in this post:

    http://our.umbraco.org/forum/developers/extending-umbraco/2225-Need-to-access-events-in-the-global?p=0

    the presented solution is through the use of IHttpModules, however for the method I want it is not possible.

    Other presented solution is by extending the class "umbraco.Global" and add it to the App_Code as presented in the post:

    http://forum.umbraco.org/yaf_postst1702_Possible-to-override-ApplicationStart--ApplicationError.aspx

    however I was not able to point to this class since there is no global.asax file, is there any way to point to it?

    how should i extend the presented methods?

    thanks in advance,

    Ricardo

  • Digby 23 posts 43 karma points
    Oct 27, 2009 @ 19:02
    Digby
    0

    Hi Ricard, I am also working on csla membership with umbraco and having similar difficulties trying to extend Application_Start

    but having a go.. let us know if you make progress.

    Thanks

    Digby

     

     

  • Ricardo Gomes 22 posts 42 karma points
    Oct 27, 2009 @ 19:16
    Ricardo Gomes
    0

    Hello Digby,

    I made a temporary solution that consists in putting the desired Application_Start code in the Init() method of an HttpModule that you can add.

    To add an HttpModule you can check this:

    http://msdn.microsoft.com/en-us/library/ms227673%28VS.80%29.aspx

    I made this since the Init() is executed more or less the same times as the Application_Start()

    hope it helps,

    let me know if you have any other solution,

    Regards,

    Ricardo

  • Nico Lubbers 151 posts 175 karma points
    Oct 27, 2009 @ 19:17
    Nico Lubbers
    1

    The global.asax.cs is compiled into a dll App_global_asax.dll which is shipped with umbraco

    Below is the global.asax.cs from the 4.0.2.1 release. You can see its almost empty. (i removed all code commented out). The solution is to delete App_global.asax.dll in your umbraco distribution and make your own global.asax.cs. I did not test this myselve, but I was confronted with this today when I tried to make log4net working on a test server (eventually that log4net can also be initiated by a HttpModule). The following blog of Ismael also states that the App_global.asax.dll may be deleted from your bin directory.

    A better solution is that in umbraco the events in the global.asax.cs can be handled by your own code. That is something the core-team might think about :-)

    Nico

    using System;
    using System.Web;
    using System.Threading;

    using umbraco.BusinessLogic;
    using umbraco.BusinessLogic.Actions;
    using umbraco.cms.businesslogic.datatype.controls;
    using umbraco.cms.businesslogic.stat;

    namespace umbraco
    {
    /// <summary>
    /// Summary description for Global.
    /// </summary>
    public class Global : HttpApplication
    {
    protected Timer publishingTimer;
    protected Timer pingTimer;

    public Global()
    {
    InitializeComponent();
    }

    protected void Session_Start(Object sender, EventArgs e)
    {
    if(GlobalSettings.EnableStat)
    {
    try
    {
    new Session();
    }
    catch(Exception state)
    {
    Log.Add(LogTypes.Error, BusinessLogic.User.GetUser(0), -1, "Error initializing stat: " + state);
    }
    }
    }


    protected void Application_EndRequest(Object sender, EventArgs e)
    {
    }

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
    }

    protected void Session_End(Object sender, EventArgs e)
    {
    }

    protected void Application_End(Object sender, EventArgs e)
    {
    Log.Add(LogTypes.System, BusinessLogic.User.GetUser(0), -1, "Application shutted down at " + DateTime.Now);
    }

    #region Web Form Designer generated code

    private void InitializeComponent()
    {
    }

    #endregion
    }
    }

     

     

     

     

  • Aaron Powell 1708 posts 3043 karma points c-trib
    Oct 28, 2009 @ 03:58
    Aaron Powell
    0

    Just make your class inherit from the umbraco.Global, that should work.

    Alternative create a HttpHander (inherit IHttpHandler) and then you have the request life cycle at your finger tips

  • Nico Lubbers 151 posts 175 karma points
    Oct 28, 2009 @ 15:23
    Nico Lubbers
    0

    @slace: can you give me an example on how to override the Application_start by inheriting unbraco.Global?

  • Aaron Powell 1708 posts 3043 karma points c-trib
    Oct 28, 2009 @ 22:29
    Aaron Powell
    1

    You should be able to do:

    protected new void Application_Start(object sender, EventArgs e) { ... }

    But really, you're better off writing your own HttpHandler, it's much more portable that way.

  • Carsten Johannesen 35 posts 55 karma points
    Mar 08, 2011 @ 11:53
    Carsten Johannesen
    0

    @slace

    tried the above - but stille nothing happens (no log entry is written) - has the umbraco functionality changed - I'm on 4.7RC

     

    Public Class MyGlobalEvents

     Inherits umbraco.Global

    Protected Overrides Sub Application_Start(ByVal sender As Object, ByVal e As System.EventArgs)

    Log.Add(LogTypes.Debug, -1, "Vi har kaldt application START by umbraco.global")

    MyBase.Application_Start(sender, e)

    End Sub

    end Class

     

  • Sebastiaan Janssen 4890 posts 14567 karma points MVP admin hq
    Mar 08, 2011 @ 12:17
    Sebastiaan Janssen
    0

    What is it that you're trying to do, log the application start? Don't bother, it's already being logged.. This is one such entry:

    id userId  NodeId  Datestamp   logHeader   logComment
    91920   0   -1  2010-08-31 10:30:51.597 System  Application started at 31-8-2010 10:30:51

    As slace says, much better to do what you're trying to do using a HttpHandler or HttpModule.

  • Carsten Johannesen 35 posts 55 karma points
    Mar 08, 2011 @ 12:38
    Carsten Johannesen
    0

    Well yes thank you ;-) - I know it's being logged already - I just inserted that line to see if MY application_start code was being triggered (which it is not).

    I wan't to start a timer which gathers performance parameters from the server at regular intervals. I have implemented this at a standard asp.net application which works beautifully - but Umbraco sites are a world apart...

    I have tried all kinds of techniques (from this forum) to catch the application_start but to no avail - I have tried overriding the umbraco.global, applicationBase, system.Web.HttpApplication but nothing seems to work...

    (some of the methods are theroretical I suspect - and not tried out) - so have anyone actually gotten this to work in umbraco 4.7 and how?

     

  • Sebastiaan Janssen 4890 posts 14567 karma points MVP admin hq
    Mar 08, 2011 @ 12:52
    Sebastiaan Janssen
    0

    Alright, did you remember to actually delete the existing App_global.asax.dll and have you put your own dll in the bin folder to replace the functionality? Is there no actual global.asax file still inheriting from umbraco.Global?

    Maybe add a global.asax that inherits from MyGlobalEvents (add your namespace before that if you have one).

    The implementation hasn't changed much in 4.7, it's even less code then it was before.

  • Carsten Johannesen 35 posts 55 karma points
    Mar 08, 2011 @ 13:20
    Carsten Johannesen
    0

    Yes have deleted app_global.asax.dll and global.asax.cs (and global.asx.vb) from the app_code

    I am copying my own NewTV2Bornholm.dll to the bin folder (using post build events in visual studio) this dll contains all my user controls and classes

    I will look into the sourcecode you linked to .... Umbraco Shall Not Beat Me :-D

  • Carsten Johannesen 35 posts 55 karma points
    Mar 08, 2011 @ 13:28
    Carsten Johannesen
    0

    @Sebastiaan

    just noticed that I don't have the  log entry from the application_end in your quoted source code in my log either....

    something strange is going on (or is that because i've deleted the app_global.asax.dll)

  • Sebastiaan Janssen 4890 posts 14567 karma points MVP admin hq
    Mar 08, 2011 @ 14:16
    Sebastiaan Janssen
    0

    I've just tried with an older version of Umbraco and it looks like this never actually worked anyway. I can't even find where the App_Global.asax.dll is generated from the umbraco source. I still get the default "application has started" message in the log, even after deleting the dll.

    Sorry that I can't be of more help! 

  • Aaron Powell 1708 posts 3043 karma points c-trib
    Mar 08, 2011 @ 23:14
    Aaron Powell
    0

    Did you change the inherits attribute in your global.asax?

    Umbraco has a class inheriting HttpApplication, umbraco.Global, which it tried to run by default.

    But yes, I've seen a number of issues trying to override global.asax, you're better off creating a HttpModule to do what you need

  • Carsten Johannesen 35 posts 55 karma points
    Mar 09, 2011 @ 10:30
    Carsten Johannesen
    0

    All Right success at last:

    Created a httpModule:

    '  the class MUST also be added to the web.config under "configuration", "system.webServer", "modules" to work
    Public Class MyGlobalEvents     Implements IHttpModule

        Public Sub Init(ByVal application As HttpApplication) Implements IHttpModule.Init
            Log.Add(LogTypes.Debug, -1, "Vi har kaldt application INIT by IHttpModule")
            If IsNothing(TV2BGlobal.appglobalObjConnectionPerfCounter) Then
                TV2BGlobal.appglobalObjConnectionPerfCounter = New TV2BGlobal.UserStatistik
            End If
        End Sub

        Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
        End Sub
    End Class

    IMPORTANT: and added this to the web.config (newTV2Bornholm is the namespace) the place where this must be added is different depending upon your IIs version - mine is 7 in integrated mode (see this for more info: http://msdn.microsoft.com/en-us/library/ms227673.aspx):

    <

    add name="MyGlobalEvents" type="NewTV2Bornholm.MyGlobalEvents, NewTV2Bornholm" />

    and this is tested and works in umbraco 4.7.

    However the INIT seems to fires several times during the lifetime of the application - so I needed to limit the number of instances of my global stat (which I only want ONE of for the entire app regardless of number of users and other variables)

  • Sebastiaan Janssen 4890 posts 14567 karma points MVP admin hq
    Mar 09, 2011 @ 10:34
    Sebastiaan Janssen
    0

    Well, I managed to do it now by using visual studio's add dialog and choosing global.asax, this gives you a default global.asax.

    But I would really go the route that Aaron is suggesting, create an httpModule, as an example look at umbraco's own umbraco.presentation.requestModule. Don't forget to add it to the web.config and you're good to go. And you can still upgrade Umbraco without worries.

  • Sebastiaan Janssen 4890 posts 14567 karma points MVP admin hq
    Mar 09, 2011 @ 10:35
    Sebastiaan Janssen
    0

    Oh, posting at the same time! :-) 

    Well, I don't know about your init but Umbraco's only fires once, so have a look at the link in the previous post anyway.

  • Ken Schnell 34 posts 144 karma points
    Aug 05, 2020 @ 18:49
    Ken Schnell
    0

    For those looking for Version > 8 information you might want to visit

    Subscribing to Events

Please Sign in or register to post replies

Write your reply to:

Draft