IHttpModule Event Threading

Discussion in 'ASP .Net' started by =?Utf-8?B?QW5kcmV3IExpcHBpdHQ=?=, Jul 5, 2004.

  1. What gaurantees are there in the way of which thread the events are called from.

    I've seen:
    Thread A Begin
    Thread B Begin
    Thread A End
    Thread A End

    That seems to indicate that Begin and End can't be assumed to be called from the same thread, but can I assume that the Begin will happen on the same thread that executes the request?

    I'm trying to use these events to log which thread is executing a request.

    Furthermore, I'm trying to do the same thing with a SOAP extension. Would anyone know the answer or should I ask in the other forums?

    Thanks,
    Andrew Lippitt
     
    =?Utf-8?B?QW5kcmV3IExpcHBpdHQ=?=, Jul 5, 2004
    #1
    1. Advertising

  2. "Andrew Lippitt" <Andrew > wrote in message
    news:...
    > What gaurantees are there in the way of which thread the events are called

    from.
    >
    > I've seen:
    > Thread A Begin
    > Thread B Begin
    > Thread A End
    > Thread A End
    >
    > That seems to indicate that Begin and End can't be assumed to be called

    from the same thread, but can I assume that the Begin will happen on the
    same thread that executes the request?
    >
    > I'm trying to use these events to log which thread is executing a request.
    >
    > Furthermore, I'm trying to do the same thing with a SOAP extension. Would

    anyone know the answer or should I ask in the other forums?

    In general, you can make no assumption about the order in which threads
    execute.

    Now, in this case, you'll need to be more specific about what you mean by
    "Begin" and "End". The IHttpModule interface doesn't define any events.
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jul 5, 2004
    #2
    1. Advertising

  3. Thanks for the quick reply!

    I'll try to elaborate. I'm implementing IHttpModule in an attempt to trap the BeginRequest and EndRequest events that the HttpApplication interface it provides exposes. My goal is to use these events to write a log indicating which pages were executed on which threads.

    What I need to know is what I can rely upon in terms of which thread these events will be fired from.

    It seems that the normal case is that EndRequest is fired from the same thread that BeginRequest is. However, the following sample will demonstrate this is not always the case. When this is installed, firing off several requests simultaneously will sometimes cause the BeginRequest and EndRequest for a single request to be fired from two different threads.

    I'd expect for example that if two requests happen simultaneously that two different threads would service them and I'd get something like:

    Thread 1: BeginRequest
    Thread 2: BeginRequest
    Thread 1: EndRequest
    Thread 2: EndRequest

    What happens in the case that seems odd is that I'll get more along the lines of

    Thread 1: BeginRequest
    Thread 2: BeginRequest
    Thread 1: EndRequest
    Thread 1: EndRequest

    My question is what CAN I assume about the threads that call these events and how they relate to the thread that is actually going to service the request. And by servicing the request, I mean can I assume that the thread that fires BeginRequest will be the thread that executes the System.Web.UI.Page that was requested?

    using System;
    using System.Web;

    public class HttpEvents : IHttpModule
    {
    public void Init(HttpApplication _context)
    {
    context.BeginRequest += new EventHandler(context_BeginRequest);
    context.EndRequest += new EventHandler(context_EndRequest);
    }

    private void context_BeginRequest(object sender, EventArgs e)
    {
    System.Diagnostics.Trace.WriteLine("Thread " + System.Threading.Thread.CurrentThread.GetHashCode() + ": BeginRequest");
    }

    private void context_EndRequest(object sender, EventArgs e)
    {
    System.Diagnostics.Trace.WriteLine("Thread " + System.Threading.Thread.CurrentThread.GetHashCode() + ": EndRequest");
    }
    }




    "John Saunders" wrote:

    > "Andrew Lippitt" <Andrew > wrote in message
    > news:...
    > > What gaurantees are there in the way of which thread the events are called

    > from.
    > >
    > > I've seen:
    > > Thread A Begin
    > > Thread B Begin
    > > Thread A End
    > > Thread A End
    > >
    > > That seems to indicate that Begin and End can't be assumed to be called

    > from the same thread, but can I assume that the Begin will happen on the
    > same thread that executes the request?
    > >
    > > I'm trying to use these events to log which thread is executing a request.
    > >
    > > Furthermore, I'm trying to do the same thing with a SOAP extension. Would

    > anyone know the answer or should I ask in the other forums?
    >
    > In general, you can make no assumption about the order in which threads
    > execute.
    >
    > Now, in this case, you'll need to be more specific about what you mean by
    > "Begin" and "End". The IHttpModule interface doesn't define any events.
    > --
    > John Saunders
    > johnwsaundersiii at hotmail
    >
    >
    >
     
    =?Utf-8?B?QW5kcmV3IExpcHBpdHQ=?=, Jul 5, 2004
    #3
  4. =?Utf-8?B?QW5kcmV3IExpcHBpdHQ=?=

    Scott Allen Guest

    Hi Andrew:

    I've never found a guarantee that a request will be on the same thread
    for the entire processing pipeline.

    --
    Scott
    http://www.OdeToCode.com

    On Mon, 5 Jul 2004 13:31:01 -0700, "Andrew Lippitt"
    <> wrote:

    >Thanks for the quick reply!
    >
    >I'll try to elaborate. I'm implementing IHttpModule in an attempt to trap the BeginRequest and EndRequest events that the HttpApplication interface it provides exposes. My goal is to use these events to write a log indicating which pages were executed on which threads.
    >
     
    Scott Allen, Jul 6, 2004
    #4
  5. =?Utf-8?B?QW5kcmV3IExpcHBpdHQ=?=

    Natty Gur Guest

    Hi,

    After HttpApplication.InitInternal set all required events that should
    be execute by application, HttpApplication.ResumeSteps actually perform
    them. Now if you use any reflection tool you will see that ResumeSteps
    guarantee that all execute steps occurred in the same thread
    (this.OnThreadEnter(); and this.OnThreadLeave();).
    (You can look at my blog for full request pipeline desc. :
    http://weblogs.asp.net/ngur/archive/2004/07/05/173301.aspx)

    Anyway correct me if I wrong but I think you record the thread that
    deals with the context event and not the thread that perform request
    pipeline.

    Natty Gur[MVP]

    blog : http://weblogs.asp.net/ngur
    Mobile: +972-(0)52-8888377


    *** Sent via Devdex http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Natty Gur, Jul 6, 2004
    #5
  6. "Andrew Lippitt" <> wrote in message
    news:...
    > Thanks for the quick reply!
    >
    > I'll try to elaborate. I'm implementing IHttpModule in an attempt to trap

    the BeginRequest and EndRequest events that the HttpApplication interface it
    provides exposes. My goal is to use these events to write a log indicating
    which pages were executed on which threads.
    >
    > What I need to know is what I can rely upon in terms of which thread these

    events will be fired from.

    You can't rely on anything at all. That's my point.
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jul 6, 2004
    #6
    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. John
    Replies:
    0
    Views:
    519
  2. Replies:
    9
    Views:
    1,048
    Mark Space
    Dec 29, 2007
  3. Steven Woody
    Replies:
    0
    Views:
    425
    Steven Woody
    Jan 9, 2009
  4. Steven Woody
    Replies:
    0
    Views:
    458
    Steven Woody
    Jan 9, 2009
  5. John

    How come I can not catch IHTTPModule Error Event???

    John, May 8, 2006, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    188
Loading...

Share This Page