ASP.Net web API: avoid race condition on data shared in HttpContext.Current.Application.Contents?

huangs3
huangs3 used Ask the Experts™
on
I am using Visual Studio 2019 to develop an ASP.Net web API. In an API controller that provides data to user, I used HttpContext.Current.Application.Contents to work as data cache between different requests and different routes. The motivation of using HttpContext.Current.Application.Contents is to cache the data fetch/computed in previously incoming request. In most of the requests the API will just get data from HttpContext.Current.Application.Contents instead of fetching them from back-end, but in some requests the cache data will be cleaned up or initialized. Because multiple requests can be handled at the same time, how can I avoid race condition on using HttpContext.Current.Application.Contents?

Thank you!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
Hi Huangs3,

I am not sure if race condition are applicable to Contents properties but I do wonder why not use Cache? https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcontext.cache?view=netframework-4.8

Regards,
Chinmay.

Author

Commented:
Hi Chinmay,

Thanks for bringing the HttpContext.Cache up. I didn't notice this before. However, after you mentioned it, I checked and found that seems HttpContext.Current.Application.Contents fits my situation more for some reasons:
1. Contents inherits from NameObjectCollectionBase class, and Cache inherits from IEnumerable class. The former seems more convenient to use.
2. I am hosting the API in a cloud service, and I don't know how whether the service provider will distribute the hosting into multiple physical server or not. According to document, Cache is for the whole application domain, such that there is one instance per application. This potentially may lead to network communication when accessing it. On another hand, HttpContext.Current.Application.Contents is not shared across different servers. that means if there will not be extra network communication, even though multiple instances may be created in cloud.

Is there a way to avoid race condition in Cache? any difference to Contents?

Author

Commented:
If there is no other idea, seems I would better to switch to creating a custom class for caching like this:
https://stackoverflow.com/questions/39562594/asp-net-web-api-how-to-create-a-persistent-collection-across-requests?rq=1

I couldn't find a way without creating custom class (such as using HttpContext.Current.Application.Contents) becuase I couldn't find a way to create an object for "lock" statement while making sure this object has the same lifetime/scope as HttpContext.Current.Application.Contents.

Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial