Catching Session start in HTTPModules

Discussion in 'ASP .Net' started by DamienS, Jul 1, 2008.

  1. DamienS

    DamienS Guest

    Hi,

    I'm trying to get away from using global.asax for reasons of
    'neatness'.

    Am I able to register a HTTPModule that captures the starting of a
    user session? Isn't Session_Start an event in global.asax? ... and
    isn't global.asax exposing events of HttpApplication?

    What am I missing?

    Thanks in advance,


    Damien



    public class clsSessionManager : IHttpModule
    {
    #region IHttpModule Members

    void IHttpModule.Dispose()
    {
    }

    void IHttpModule.Init(HttpApplication context)
    {
    // I would have thought that I could use
    context.SessionStart+= here....
    }

    #endregion
    }
    DamienS, Jul 1, 2008
    #1
    1. Advertising

  2. I put all kinds of code , static methods and fields in Global.asax and it is
    very neat (at least to me).
    You can use Session in an HttpModule by implementing the marker interface
    IRequiresSessionState.
    Peter
    "DamienS" <> wrote in message
    news:...
    > Hi,
    >
    > I'm trying to get away from using global.asax for reasons of
    > 'neatness'.
    >
    > Am I able to register a HTTPModule that captures the starting of a
    > user session? Isn't Session_Start an event in global.asax? ... and
    > isn't global.asax exposing events of HttpApplication?
    >
    > What am I missing?
    >
    > Thanks in advance,
    >
    >
    > Damien
    >
    >
    >
    > public class clsSessionManager : IHttpModule
    > {
    > #region IHttpModule Members
    >
    > void IHttpModule.Dispose()
    > {
    > }
    >
    > void IHttpModule.Init(HttpApplication context)
    > {
    > // I would have thought that I could use
    > context.SessionStart+= here....
    > }
    >
    > #endregion
    > }
    Peter Bromberg [C# MVP], Jul 1, 2008
    #2
    1. Advertising

  3. DamienS

    DamienS Guest

    Thanks very much Peter.

    I'm having trouble finding the session_start event though. It appears
    in global.asax, however I can't find any other reference to it in the
    framework. I've read up on IRequiresSessionState and understand that I
    can use it to see if the current context requires a session, however
    what I'm trying to do is capture when a session begins. Is there any
    way to do this outside of global.asax?

    After digging around some more, I'm now really curious - 'where' is
    the session_start event in global.asax defined? Why doesn't it appear
    in the object model? I notice that there's a AcquireRequestState event
    on HttpApplication described as "Occurs when ASP.NET acquires the
    current state (for example, session state) that is associated with the
    current request." - however, to me, this description doesn't say that
    it fires when a new session is created.

    Thanks in advance,


    Damien




    On Jul 1, 1:07 pm, "Peter Bromberg [C# MVP]"
    <> wrote:
    > I put all kinds of code , static methods and fields in Global.asax and it is
    > very neat (at least to me).
    > You can useSessionin an HttpModule by implementing the marker interface
    > IRequiresSessionState.
    > Peter"DamienS" <> wrote in message
    >
    > news:...
    >
    >
    >
    > > Hi,

    >
    > > I'm trying to get away from using global.asax for reasons of
    > > 'neatness'.

    >
    > > Am I able to register a HTTPModule that captures the starting of a
    > > usersession? Isn't Session_Start an event in global.asax? ... and
    > > isn't global.asax exposing events of HttpApplication?

    >
    > > What am I missing?

    >
    > > Thanks in advance,

    >
    > > Damien

    >
    > > public class clsSessionManager : IHttpModule
    > >    {
    > >        #region IHttpModule Members

    >
    > >        void IHttpModule.Dispose()
    > >        {
    > >        }

    >
    > >        void IHttpModule.Init(HttpApplication context)
    > >        {
    > >           // I would have thought that I could use
    > > context.SessionStart+= here....
    > >        }

    >
    > >        #endregion
    > >    }- Hide quoted text -

    >
    > - Show quoted text -
    DamienS, Jul 2, 2008
    #3
  4. DamienS

    Norm Guest

    On Jul 1, 4:06 pm, DamienS <> wrote:
    > Thanks very much Peter.
    >
    > I'm having trouble finding the session_start event though. It appears
    > in global.asax, however I can't find any other reference to it in the
    > framework. I've read up on IRequiresSessionState and understand that I
    > can use it to see if the current context requires a session, however
    > what I'm trying to do is capture when a session begins. Is there any
    > way to do this outside of global.asax?
    >
    > After digging around some more, I'm now really curious - 'where' is
    > the session_start event in global.asax defined? Why doesn't it appear
    > in the object model? I notice that there's a AcquireRequestState event
    > on HttpApplication described as "Occurs when ASP.NET acquires the
    > current state (for example, session state) that is associated with the
    > current request." - however, to me, this description doesn't say that
    > it fires when a new session is created.
    >
    > Thanks in advance,
    >
    > Damien
    >
    > On Jul 1, 1:07 pm, "Peter  Bromberg [C# MVP]"
    >
    > <> wrote:
    > > I put all kinds of code , static methods and fields in Global.asax and it is
    > > very neat (at least to me).
    > > You can useSessionin an HttpModule by implementing the marker interface
    > > IRequiresSessionState.
    > > Peter"DamienS" <> wrote in message

    >
    > >news:...

    >
    > > > Hi,

    >
    > > > I'm trying to get away from using global.asax for reasons of
    > > > 'neatness'.

    >
    > > > Am I able to register a HTTPModule that captures the starting of a
    > > > usersession? Isn't Session_Start an event in global.asax? ... and
    > > > isn't global.asax exposing events of HttpApplication?

    >
    > > > What am I missing?

    >
    > > > Thanks in advance,

    >
    > > > Damien

    >
    > > > public class clsSessionManager : IHttpModule
    > > >    {
    > > >        #region IHttpModule Members

    >
    > > >        void IHttpModule.Dispose()
    > > >        {
    > > >        }

    >
    > > >        void IHttpModule.Init(HttpApplication context)
    > > >        {
    > > >           // I would have thought that I could use
    > > > context.SessionStart+= here....
    > > >        }

    >
    > > >        #endregion
    > > >    }- Hide quoted text -

    >
    > > - Show quoted text -


    Damien,
    The Session is implemented via a HttpModule itself. It is registered
    by default in the root web.config. It is of the type
    System.Web.SessionState.SessionStateModule. That class is what exposes
    the session start event. In order to bind an event handler you will
    have to obtain the instance of the SessionStateModule. The
    HttpApplication exposes the modules as a collection. So your code
    would look something like below. (I am a VB developer so excuse syntax
    etc. ) Hopefully this helps.

    void IHttpModule.Init(HttpApplication context)
    {
    SessionStateModule sessionModule = context.modules("Session");
    sessionModule.Start += here ...
    }
    Norm, Jul 2, 2008
    #4
  5. DamienS

    Norm Guest

    On Jul 1, 6:28 pm, Norm <> wrote:
    > On Jul 1, 4:06 pm, DamienS <> wrote:
    >
    >
    >
    > > Thanks very much Peter.

    >
    > > I'm having trouble finding the session_start event though. It appears
    > > in global.asax, however I can't find any other reference to it in the
    > > framework. I've read up on IRequiresSessionState and understand that I
    > > can use it to see if the current context requires a session, however
    > > what I'm trying to do is capture when a session begins. Is there any
    > > way to do this outside of global.asax?

    >
    > > After digging around some more, I'm now really curious - 'where' is
    > > the session_start event in global.asax defined? Why doesn't it appear
    > > in the object model? I notice that there's a AcquireRequestState event
    > > on HttpApplication described as "Occurs when ASP.NET acquires the
    > > current state (for example, session state) that is associated with the
    > > current request." - however, to me, this description doesn't say that
    > > it fires when a new session is created.

    >
    > > Thanks in advance,

    >
    > > Damien

    >
    > > On Jul 1, 1:07 pm, "Peter  Bromberg [C# MVP]"

    >
    > > <> wrote:
    > > > I put all kinds of code , static methods and fields in Global.asax and it is
    > > > very neat (at least to me).
    > > > You can useSessionin an HttpModule by implementing the marker interface
    > > > IRequiresSessionState.
    > > > Peter"DamienS" <> wrote in message

    >
    > > >news:....

    >
    > > > > Hi,

    >
    > > > > I'm trying to get away from using global.asax for reasons of
    > > > > 'neatness'.

    >
    > > > > Am I able to register a HTTPModule that captures the starting of a
    > > > > usersession? Isn't Session_Start an event in global.asax? ... and
    > > > > isn't global.asax exposing events of HttpApplication?

    >
    > > > > What am I missing?

    >
    > > > > Thanks in advance,

    >
    > > > > Damien

    >
    > > > > public class clsSessionManager : IHttpModule
    > > > >    {
    > > > >        #region IHttpModule Members

    >
    > > > >        void IHttpModule.Dispose()
    > > > >        {
    > > > >        }

    >
    > > > >        void IHttpModule.Init(HttpApplication context)
    > > > >        {
    > > > >           // I would have thought that I could use
    > > > > context.SessionStart+= here....
    > > > >        }

    >
    > > > >        #endregion
    > > > >    }- Hide quoted text -

    >
    > > > - Show quoted text -

    >
    > Damien,
    > The Session is implemented via a HttpModule itself. It is registered
    > by default in the root web.config. It is of the type
    > System.Web.SessionState.SessionStateModule. That class is what exposes
    > the session start event. In order to bind an event handler you will
    > have to obtain the instance of the SessionStateModule. The
    > HttpApplication exposes the modules as a collection. So your code
    > would look something like below. (I am a VB developer so excuse syntax
    > etc. ) Hopefully this helps.
    >
    > void IHttpModule.Init(HttpApplication context)
    > {
    >     SessionStateModule sessionModule = context.modules("Session");
    >     sessionModule.Start += here ...
    >
    > }


    Clarification: "Session" is the name given to the SessionStateModule
    when it is registered in the root web.config.
    Norm, Jul 2, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Turner

    Virtual Paths and HttpModules

    Ian Turner, Oct 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    365
    Dmitry Andreev
    Oct 26, 2003
  2. Alvin Bruney
    Replies:
    1
    Views:
    721
  3. Aurel

    HttpModules et session

    Aurel, Nov 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    3,438
    Aurel
    Nov 30, 2003
  4. Amil Hanish
    Replies:
    0
    Views:
    552
    Amil Hanish
    Apr 13, 2006
  5. Amil Hanish
    Replies:
    0
    Views:
    299
    Amil Hanish
    Apr 18, 2006
Loading...

Share This Page