SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Session_OnStart in base class AND global.asax

    Hi,

    I have made my own Application class which inherits from HttpApplication.

    Within this class I make use of the events Session_OnStart, Application_OnStart & Application_OnEnd.

    However this hides the events from global.asax whereas actually I would still like to make use of them there.

    If it was a regular event handler I could register as many as I want and it would fire for all of them. But I can't work out how to do it for these events.

    Any suggestions?

    Cheers,

    Dale
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  2. #2
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just override in your class and make sure you also call the base method.

  3. #3
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well if you could tell me how to do that please as best I can tell those particular events are not available to override nor to call via the base class.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  4. #4
    ALT.NET - because we need it silver trophybronze trophy dhtmlgod's Avatar
    Join Date
    Jul 2001
    Location
    Scotland
    Posts
    4,836
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your base class, make each method virtual, then in the child class, override it and call the base method if you still need it to do something:
    Code Csharp:
    public class Application : HttpApplication
        {
            protected virtual void Session_OnStart()
            {
     
            }
        }
     
        public class ApplicationImpl : Application
        {
            protected override void Session_OnStart()
            {
                // Do what you need
                base.Session_OnStart();
            }
        }

  5. #5
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Can you have multiple HttpApplication classes running around in an app? Anyhow, better bet would be to actually patch into the event, which is fired by the System.Web.SessionStateModule. Getting at the module is pretty easy--just grab the Module from the Modules collection of the application and attach events as appropriate:

    Code:
    SessionStateModule ssm = Modules["Session"] as SessionStateModule
    if (ssm!=null)
    {
        ssm.Start += MyStartEventHandler;
        ssm.End += MyEndEventHandler;
    }
    Those xyz_OnWhatever things are holdovers for old ASP devs who were transitioning to the new model.

  6. #6
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks wwb_99 - thats the approach I was trying to take but I couldn't work out where to register the event listeners, and I still can't work out where I can attach the Application OnStart/OnEnd events?

    Also out of interest, is there a reference as to what modules I would expect to find in the modules collection?
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The default modules are registered in the machine's web.config (found @ %SYSTEMROOT%\Microsoft.NET\Framework\[version]\CONFIG\web.config). If you want runtime info, you could always enumerate the HttpApplication.Modules property.

    What it sounds like you want to do here is write a custom IHttpModule, not extend HttpApplication. It is a much, much cleaner approach.

  8. #8
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the info wwb_99 - I'm not sure if I follow correctly, but from what I could understand of writing my own custom HttpModule, that seemed to be contained within the Application context, and would therefore not give me any more control over the Application OnStart/OnEnd events. Have I misunderstood? For now I'm using the Application.Init()/.Dispose() events which just means I get a few more than I really need. Cheers
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  9. #9
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,653
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The IHttpModule has an Init method which is where you can tell it how to handle the Application's Start and End events. A module doesn't give you "more control" but rather it has it's own lifecycle.

    What are you trying to accomplish here?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •