System.Web.HttpContext.Current shuts down after checking cache

Today I ran into a strange problem that made no sense to me. Here is a summary:

Inside the method, I check the cached element as shown 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 is HttpContext.Currentnot null as expected in an ASP.NET application. Then, inside the method cacheRatesStatusObject, I check to see if it HttpContext.Currentis null or not:

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 he is there null. I don’t know what is going on here. Any thoughts?

+5
source share
3 answers

async/await, , , , ASP.NET thread pool. , , HttpContext , .

HttpContext , :

await cacheRatesStatusObject(HttpContext.Current,  ratesStatus);

concurrency , , , , . HttpContext .

+4

.

HttpContext stored only in a static stream.

As suggested by another answer, just pass the instance.

0
source

All Articles