Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.HttpContext.Current nulls itself after checking for a Cache

I encountered a weird issue today which made no sense to me. Here is a summary:

Inside a method, I check for a cached item as below:

private async Task<RatesStatus> getRatesStatusAsync() {

    //...

    if (_currentHttpContext != null) {

        //Here, I am checking for a Cached item
        var cachedRatesStatusObj = HttpContext.Current.Cache[Constants.RATESSTATUS_CACHE_KEY_NAME];
        if (cachedRatesStatusObj != null)
            return (RatesStatus)cachedRatesStatusObj;
    }

    //...

    cacheRatesStatusObject(ratesStatus);

    //...
}

Here, the HttpContext.Current is not null as expected inside an ASP.NET application. Then, inside the cacheRatesStatusObject method, I check if HttpContext.Current is null or not as below:

private void cacheRatesStatusObject(RatesStatus ratesStatus) {

    //...

    //Seeing if HttpContext.Current is null or not first.
    //and it is null here...
    if (HttpContext.Current == null)
        return;

    //...
}

And it is null there. No idea what is happening here. Any thoughts?

like image 988
tugberk Avatar asked May 11 '12 15:05

tugberk


People also ask

Why HttpContext current is null?

Clearly HttpContext. Current is not null only if you access it in a thread that handles incoming requests. That's why it works "when i use this code in another class of a page".

How does HttpContext current work?

The HttpContext encapsulates all the HTTP-specific information about a single HTTP request. When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.

Where is HttpContext items stored?

It is stored in the memory of the server and the value is available for the entire lifetime of the request.


1 Answers

When you use async/await, the thread handling the request marks the request as incomplete and then returns to the ASP.NET thread pool. When the awaitable completes later, another thread is assigned to run the rest of the method, however HttpContext is not migrated across threads, that's why you get null reference when calling await method.

You can pass a reference of the HttpContext to the await method, something like this:

await cacheRatesStatusObject(HttpContext.Current,  ratesStatus);

However you should be very careful dealing with concurrency and race conditions, for example if the await thread locks a resource and another request thread attempts to use it then your thread pool goes boom. Most people resolve this by creating new objects and passing them into paramaterized threads instead of passing a reference of HttpContext across threads.

like image 85
Kamyar Nazeri Avatar answered Oct 13 '22 10:10

Kamyar Nazeri