Polly: Cache policy gives unhelpful exception on sync/async config/execution mismatch

Created on 26 May 2018  路  8Comments  路  Source: App-vNext/Polly

I am following the samples to implement cache but I'm getting an exception during the call:

This is the policiy definition:

var cachePolicy = Policy.Cache(new MemoryCacheProvider(MemoryCache.Default), TimeSpan.FromMinutes(5))

Then this is the call:

return cachePolicy.ExecuteAsync(async () => await _webService.someMethodAsync());

And this is the error it throws:

Value cannot be null. Parameter name: nonGenericCacheProvider" at Polly.Caching.GenericCacheProviderAsync'1..ctor(IAsyncCacheProvider nonGenericCacheProvider)

What am I missing? I think this pretty much matches the samples offered in the Readme.md, but still no idea how to get rid of that exception.

BTW: the ExecuteAsync overload to set Context and set the operationKey shows as deprecated, something not mentioned in documentation.

question

All 8 comments

@CesarD For asynchronous executions using ExecuteAsync(...), the policy must be defined as an asynchronous policy, thus:

var cachePolicy = Policy.CacheAsync(new MemoryCacheProvider(MemoryCache.Default), TimeSpan.FromMinutes(5));

All policies should give a helpful exception message explaining that, if sync/async are mixed incorrectly, but this case has escaped that. I'll fix to make sure it gives a helpful exception.

Thanks for spotting the deprecated overload quoted in the cache documentation: it'll be corrected in both the readme and wiki.

Fixes in https://github.com/reisenberger/Polly/commit/56a0e488ae534754d7618ef26d5569b44b5aaff4 . Will be PRd after some other material has been added to v6.1.0

Thanks for answering @reisenberger.
Now I don't get the exception and the code works, but it doesn't cache the results. The call to webservice is executed on every request.
I have configured the policies on a class that I inject later on my controllers, and I registered the DI for that class as a Singleton. Anything else I might be missing? Thanks!

Oh, btw: what should be used instead of the deprecated overload? How should I set a cache key now?
Thanks!

Hi @CesarD

Now I don't get the exception and the code works, but it doesn't cache the results. The call to webservice is executed on every request

If the cache get or put encounters an exception, the exception is captured and passed to a configurable delegate rather than bringing down your whole app or operation; and the underlying operation (webservice in your case) is invoked. If you are seeing the webservice continually invoked, perhaps something is failing with cache get or put. Try attaching a delegate to one of the policy error hooks to capture what may be happening.

what should be used instead of the deprecated overload?

cachePolicy.ExecuteAsync(async context => await _webService.someMethodAsync(), new Context("CacheKeyToUse"));

I modified the policy configuration like this:

cachePolicy = Policy.CacheAsync(new MemoryCacheProvider(MemoryCache.Default), TimeSpan.FromMinutes(5),
                (context, s) =>
                {
                    Console.WriteLine("get");
                }, (context, s) =>
                {
                    Console.WriteLine("miss");
                }, (context, s) =>
                {
                    Console.WriteLine("put");
                }, (context, s, ex) =>
                {
                    Console.WriteLine("getError");
                }, (context, s, ex) =>
                {
                    Console.WriteLine("putError");
                });

WebService is called every time and responds with the results, and I put breakpoints on the console calls but none of them are captured.
Sorry, I'm on a loss here...

Never mind, I just figured it out: I needed to pass the Context parameter with the cache key on the ExecuteAsync method for all the stuff to work. Didn't seem to be required, but it is... Perhaps a note on docs to emphasize it more?
Thanks and keep up the good work ;)

@CesarD Thanks for the feedback. Extra clarification added:

Was this page helpful?
0 / 5 - 0 ratings