@@ -64,24 +64,24 @@ public class Startup
6464 public void ConfigureServices (IServiceCollection services )
6565 {
6666 services .AddMemoryCache ();
67- services .AddSingleton <Polly .Registry .IPolicyRegistry <string >, Polly .Registry .PolicyRegistry >();
68- services .AddSingleton <Polly .Caching .ISyncCacheProvider , Polly .Caching .Memory .MemoryCacheProvider >(); // Or: IAsyncCacheProvider
69- // ...
70- }
67+ services .AddSingleton <Polly .Caching .IAsyncCacheProvider , Polly .Caching .Memory .MemoryCacheProvider >();
68+
69+ services .AddSingleton <Polly .Registry .IPolicyRegistry <string >, Polly .Registry .PolicyRegistry >((serviceProvider ) =>
70+ {
71+ PolicyRegistry registry = new PolicyRegistry ();
72+ registry .Add (" myCachePolicy" , Policy .CacheAsync <HttpResponseMessage >(serviceProvider .GetRequiredService <IAsyncCacheProvider >(), TimeSpan .FromMinutes (5 )));
73+ return registry ;
74+ });
7175
72- public void Configure (..., IPolicyRegistry <string > policyRegistry , ISyncCacheProvider cacheProvider )
73- {
74- var cachePolicy = Policy .Cache (cacheProvider , TimeSpan .FromMinutes (5 )); // Or: Policy.CacheAsync(IAsyncCacheProvider, ...)
75- policyRegistry .Add (" myCachePolicy" , cachePolicy );
7676 // ...
7777 }
7878}
7979
8080// In a controller, inject the policyRegistry and retrieve the policy:
81- // (magic string "myCachePolicy" only hard-coded here to keep the example simple! )
81+ // (magic string "myCachePolicy" only hard-coded here to keep the example simple)
8282public MyController (IPolicyRegistry < string > policyRegistry )
8383{
84- var _cachePolicy = policyRegistry .Get <ISyncPolicy > (" myCachePolicy" ); // Or: IAsyncPolicy
84+ var _cachePolicy = policyRegistry .Get <IAsyncPolicy < HttpResponseMessage >> (" myCachePolicy" );
8585 // ...
8686 }
8787
0 commit comments