3

I own both WebApi server (asp.net core app) and the client (UWP app).

I call the WebApi services using HttpClient from the UWP app.

Some resources are readonly and therefore can be cached:

[ResponseCache(Duration = 60*60*12, VaryByQueryKeys = new[] { "id" }, Location = ResponseCacheLocation.Client)]`
[HttpGet("{id}")]
public IActionResult Get(string id) { ... }

Is it possible to enable caching in HttpClient in UWP app or do I have to do it on my own?

Liero
  • 25,216
  • 29
  • 151
  • 297
  • What kind of caching are you looking to use? There are external libraries that adds an easy way to cache on the client if you don't want to use the simple built-on functionality. Examples: [CacheCow](https://github.com/aliostad/CacheCow) and [Cashew](https://github.com/joakimskoog/Cashew) – Joakim Skoog Aug 28 '17 at 10:22

1 Answers1

0

The HttpClient has (good) caching itself already. If the default caching behavior is not enough, you can further control the cache through the HttpCacheControl class which separates read and write behavior.

More important is knowing how to use your HttpClient. Even though it implements IDisposable, you should NOT dispose it but keep a single HttpClient object alive through your whole application, it's designed for re-use.

What the HttpClient doesn't do, is return you the cached result while being disconnected. Therefore there are other libraries like Akavache, which creates an offline key-value store.

Bart
  • 9,925
  • 7
  • 47
  • 64
  • I do not dispose HttpClient, I use singleton, but it still makes the same request to web server (observed trought Fiddler). Why? – Liero Aug 14 '17 at 12:20
  • BTW: you can set the cache flags on HttpClient's HttpBaseProtocolFilter to get data only from the cache. It's specifically designed for handling the "no network" case. (But always keep in mind that http caches aren't a database! the system might well decide to remove stuff from the cache or never add it to the cache. You know, for Reasons). – PESMITH_MSFT Jun 26 '18 at 00:33