Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / October 2005

Tip: Looking for answers? Try searching our database.

Multi-threaded HTTP Module

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GWiz - 16 Oct 2005 05:59 GMT
All,

I am trying to write a multi-threaded HTTP module that handles the
OnBeginRequest and logs it. What I am trying to do is to offload the
logging to another thread which will allow the http module to release
the OnBeginRequest.

The issue that I have is that I keep on getting "Request is not
available in this context" exceptions.

The following is the code I am using:

using System;
using System.Web;

public class ProcessRequest
{
    public HttpApplication HttpRequest;

    public void ProcessingThread()
    {
        try
        {
            string sRequestUrl;
            if (HttpRequest.Request!=null)
            {
                sRequestUrl=HttpRequest.Request.HttpMethod;
                System.Diagnostics.Debug.WriteLine(sRequestUrl);
            }
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
    }
}

public class HttpRequestLogger : IHttpModule
{
    protected System.Collections.Queue oHttpRequestQueue= new
System.Collections.Queue();
    protected bool bModuleRunning =false;

    public HttpRequestLogger()
    {
    }

    // In the Init function, register for HttpApplication
    // events by adding your handlers.
    public void Init(HttpApplication application)
    {
        application.BeginRequest +=
            (new EventHandler(this.Application_BeginRequest));
        application.EndRequest +=
            (new EventHandler(this.Application_EndRequest));
    }

    private void Application_BeginRequest(Object source, EventArgs e)
    {
        // Create HttpApplication and HttpContext objects to access
        // request and response properties.
        System.Threading.Thread oProcessingThread;
        HttpApplication application = (HttpApplication)source;
        ProcessRequest oRequestProcessor=new ProcessRequest();
        oRequestProcessor.HttpRequest=application;
        oProcessingThread =new System.Threading.Thread(new
System.Threading.ThreadStart(oRequestProcessor.ProcessingThread));
        oProcessingThread.Start();
    }

    private void Application_EndRequest(Object source, EventArgs e)
    {
//        HttpApplication application = (HttpApplication)source;
//        HttpContext context = application.Context;
    }

    public void Dispose()
    {
    }
}
Scott Allen - 16 Oct 2005 16:09 GMT
Hi GWiz:

It's possible that between the time you set the HttpRequest property
(oRequestProcessor.HttpRequest=application;) and the time the thread
actually runs, that the original request will run to completion. It
could be that the ASP.NET runtime has cleared out all the data
structures for the request you are trying to log.

My suggestion would be to start simple and not use a second thread.
Even if the logging is a bottleneck, creating a new thread for each
request is certainly going to be more overhead and you might bog down
the server even more.

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

>All,
>
[quoted text clipped - 76 lines]
>    }
>}
John Timney ( MVP ) - 16 Oct 2005 23:35 GMT
Your not passing the context to the thread, and probably cant as Scott
suggest, because of the duration of the request not remaining in scope.  You
would probably have to take the values from the request and pass those to
the thread, rather than relying on context.

Have a read of this excellent article for some ideas:

http://www.west-wind.com/presentations/howaspnetworks/howaspnetworks.asp

Signature

Regards

John Timney
ASP.NET MVP
Microsoft Regional Director

> Hi GWiz:
>
[quoted text clipped - 93 lines]
>> }
>>}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.