How do I associate some custom data with current HttpRequest?

.net, asp.net, c#, iis

Solution

This should work - I have done this in a project before.

I have a class which has a static property like this -

public class AppManager
{
    public static RequestObject RequestObject
    {
        get
        {
            if (HttpContext.Current.Items["RequestObject"] == null)
            {
                HttpContext.Current.Items["RequestObject"] = new RequestObject();
            }

            return (RequestObject)HttpContext.Current.Items["RequestObject"];
        }
        set { HttpContext.Current.Items["RequestObject"] = value; }
    }
}

And then RequestObject contains all my custom data so then in my app I can do

AppManager.RequestObject.CustomProperty

So far I have not come across any issues in the way HttpContext.Items works.

Problem

I need to somehow attach my custom data to the `HttpRequest` being handled by my IIS custom modules - so that code that runs in earlier stages of IIS pipeline attaches an object and code that runs in later stages can retrieve the object and use it and no other functionality of IIS pipeline processing is altered by adding that object. The data needs to persist within one HTTP request only - I don't need it to be stored between requests. I need it to be "reset" for each new request automatically - so that when a new request comes it doesn't contain objects my code attached to the previous request. Looks like `HttpContext.Items` is the way to go, although MSDN description of its purpose is not very clear. Is using `HttpContext.Current.Items` the way to solve my problem?

Original source