Skip to content

Commit 262dd9c

Browse files
committed
Improve DI example in readme
1 parent 9cafa60 commit 262dd9c

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,24 +63,24 @@ public class Startup
6363
public void ConfigureServices(IServiceCollection services)
6464
{
6565
services.AddMemoryCache();
66-
services.AddSingleton<Polly.Registry.IPolicyRegistry<string>, Polly.Registry.PolicyRegistry>();
67-
services.AddSingleton<Polly.Caching.ISyncCacheProvider, Polly.Caching.MemoryCache.MemoryCacheProvider>(); // Or: IAsyncCacheProvider
68-
// ...
69-
}
66+
services.AddSingleton<Polly.Caching.IAsyncCacheProvider, Polly.Caching.Memory.MemoryCacheProvider>();
67+
68+
services.AddSingleton<Polly.Registry.IPolicyRegistry<string>, Polly.Registry.PolicyRegistry>((serviceProvider) =>
69+
{
70+
PolicyRegistry registry = new PolicyRegistry();
71+
registry.Add("myCachePolicy", Policy.CacheAsync<HttpResponseMessage>(serviceProvider.GetRequiredService<IAsyncCacheProvider>(), TimeSpan.FromMinutes(5)));
72+
return registry;
73+
});
7074

71-
public void Configure(..., IPolicyRegistry<string> policyRegistry, ISyncCacheProvider cacheProvider)
72-
{
73-
var cachePolicy = Policy.Cache(cacheProvider, TimeSpan.FromMinutes(5)); // Or: Policy.CacheAsync(IAsyncCacheProvider, ...)
74-
policyRegistry.Add("myCachePolicy", cachePolicy);
7575
// ...
7676
}
7777
}
7878

7979
// In a controller, inject the policyRegistry and retrieve the policy:
80-
// (magic string "myCachePolicy" only hard-coded here to keep the example simple!)
80+
// (magic string "myCachePolicy" only hard-coded here to keep the example simple)
8181
public MyController(IPolicyRegistry<string> policyRegistry)
8282
{
83-
var _cachePolicy = policyRegistry.Get<ISyncPolicy>("myCachePolicy"); // Or: IAsyncPolicy
83+
var _cachePolicy = policyRegistry.Get<IAsyncPolicy<HttpResponseMessage>>("myCachePolicy");
8484
// ...
8585
}
8686

0 commit comments

Comments
 (0)