I have code running in ApiController (ASP.Net Web API) that itself wants to make a GET request to another web service. The web service (also part of my application) returns Cache-Control headers indicating the expiration time for the returned content.
I am using the new System.Net.Http.HttpClient
configured with WebRequestHandler
to use client-side caching (by default, HttpClientHandler
does not support cache configuration, although it uses System.Net.WebRequest
as its basic HTTP implementation):
var client = new HttpClient(new WebRequestHandler { UseDefaultCredentials = true, CachePolicy = new RequestCachePolicy(RequestCacheLevel.Default) }); var response = client.GetAsync("someUri").Result; response.EnsureSuccessStatusCode();
On the server, I enable caching inside my action with ...
var response = new HttpResponseMessage(HttpStatusCode.OK); response.Headers.CacheControl = new CacheControlHeaderValue { Public = true, MaxAge = new TimeSpan(0, 5, 0); // Five minutes in this case }; // Omitted, some content is added to the response return response;
The above (shortened) code works correctly in the test; I make several calls to the service this way, and only the first call actually connects to the service (observed through log messages on the service in IIS); subsequent calls use the cache.
However, when running the same code hosted in IIS itself, it seems that the HttpClient
ignores the caching result (I also configured my IoC container so that there is only one HttpClient
instance in AppDomain) and calls the service each time. This works like an AppPoolIdentity.
Interestingly, if I change the application pool to work as a NetworkService, then the response will have a status code of 401 Unauthorized (I tried setting Preauthenticate = true
to WebRequestHandler
, but the status code is still 401). The same is true if I change the application pool to run under my own credentials.
So, is there something to run the application pool under the NetworkService identifier and virtual AppPoolIdentity, which prevents them from using client-side caching. Where in any case does the content cached by WebRequest
physically exist?