Expert

Coen Adrien van Driel

I'm a software architect and software quality & testing enthusiast. My focus is on using Microsoft technologies from C# to Azure to create awesome products.

I'm also the founder of ExpertSwitch, a software consulting company.

Disabling Caching For System.Net.WebClient In .NET

April 03, 2019

Caching is great, until it’s not. Like when you keep getting the old data from an API even though the underlying data has been updated.



While in the process of steadily moving our ASP.NET MVC application from the monolith structure to a microservices architecture, we ran into an issue when we tried to call our first ASP.NET Core API microservice. From our MVC application we initiated a GET-call using the WebClient to our newly developed API, which would then process some data in the API backend and return the latest metric-data. However, for some reason, the API would always return the old data, or so it seemed.

Clearly somewhere, some cache was being read instead of the actual API data. As something that seemed like a quick fix, required quite a workaround to bypass the caching system.

The first step was to locate which caching system was causing the issue, it could be the WebClient calling the API, the API itself (ASP.NET Core MVC) or the unlikely case of IIS caching. We ended up disabling caching on both the WebClient and the API level as I will demonstrate below.

Disabling WebClient Cache

The WebClient has a setting for its caching behavior and disabling can be done as per code below.

    using (var webClient = new WebClient())
    {
        webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
        fileData = webClient.DownloadData(restUrl);
    }

The catch is that changing this setting did not actually disable the caching in our case. In order to force the WebClient to initiate each call without cache, we added a random number to the REST-URL. This number meant a different URL was used for each request. As for the API backend, considering this additional number parameter does not exist in the method signature, it is ignored. In case this still does not work, the alternative could be to abandon the WebClient all together and initiate your API calls using other methods like using the HttpClient or HttpWebRequest class.


    Random random = new Random(); // workaround to disable webclient caching
    string restUrl = $"{ApiUrl}/api/getdata?id={id}&randomvalue{random.Next().ToString()}";

Disabling ASP.NET Core MVC Cache

Then comes the possibility that caching was done by the API itself instead of the WebClient. In that case, adding the caching-attribute to the action or controller can disable caching on the API side of things.

[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]

Thanks for reading!

If you want to stay in the loop when I post new articles, follow me on facebookFacebook or twitterTwitter Was this article helpful or do you have any questions? Leave a comment!

Check out other similar articles