问题
This is a follow up to this question regarding caching responses to an HttpWebRequest
.
I have a WebApi server that returns a JSON response with an Expires header that indicates how long the response should be cached locally on the client.
I have written a .Net Standard 2.0 library with code to enable a default RequestCachePolicy as follows:
var policy = new RequestCachePolicy(RequestCacheLevel.Default);
HttpWebRequest.DefaultCachePolicy = policy;
var request = WebRequest.Create(url);
... etc, a typical HttpWebRequest exchange ...
I call this .NET Standard library from a .NET Framework 4.6.1 Console application. Caching works as I would expect:
- If I make the same request multiple times, from the same or different instances of the console application, all requests after the first are served from the local cache until the time in the Expires header is reached, after which a new request is sent to the server.
This is what I would expect, as according to this answer, it uses Microsoft.Win32.WinInetCache
which uses WinInet functions for caching, so that data is cached in a cache shared between multiple processes (as well as with IE if I understand correctly).
If I run exactly the same .NET Standard 2.0 code from a .NET Core console application, there is no caching and each request goes to the server.
It's understandable that .NET Core doesn't use Microsoft.Win32.WinInetCache
, for portability reasons. Is there a way that I can get automatic caching of responses in .NET Core (e.g. by injecting a caching implementation using configuration).
I'm interested in solutions for .NET Core 2.x on Windows and on Linux. Ideally the cached data would be shared between multiple processes, like it is on .NET Framework.
Can anyone help?
来源:https://stackoverflow.com/questions/55856082/how-to-enable-response-caching-with-httpwebrequest-on-net-core