New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the new overloads that accept lambda that configures the options #1177
Conversation
Seems a reasonable approach to me. I would imagine that it should compose well with DI so that people could load configuration settings from configuration? For example: builder.AddRetry(o =>
{
o.RetryCount = configuration.Get<int>("MyRetryCount");
}); |
It's one more overload for each strategy, but I think it makes the configuration of the predicates/events/generators much more readable |
For DI scenario I would recommend this approach (still does not negate the approach above): return new ServiceCollection()
.Configure<RetryStrategyOptions>(options =>
{
o.RetryCount = 4;
o.BackoffType = RetryBackoffType.Exponential;
o.ShouldRetry.HandleResult(-1).HandleException<InvalidOperationException>();
o.OnRetry.Register(() => Console.WriteLine("Retry occurred!"));
})
.AddResilienceStrategy("my-retry", context =>
{
// resolve options from DI
var options = context.ServiceProvider.GetRequiredService<IOptions<RetryStrategyOptions>>().Value;
context.Builder.AddRetry(options);
}) You can just resolve complete configured options from DI. |
OK in my use case then I'd probably not use the new overload and do it like your example, as I would want some values to be in external configuration (e.g. JSON files etc.) not hard-coded. return new ServiceCollection()
.Configure<RetryStrategyOptions, IConfiguration>((o, configuration) =>
{
o.RetryCount = configuration.Get<int>("MyRetryCount");
o.BackoffType = RetryBackoffType.Exponential;
o.ShouldRetry.HandleResult(-1).HandleException<InvalidOperationException>();
o.OnRetry.Register(() => Console.WriteLine("Retry occurred!"));
})
.AddResilienceStrategy("my-retry", context =>
{
// resolve options from DI
var options = context.ServiceProvider.GetRequiredService<IOptions<RetryStrategyOptions>>().Value;
context.Builder.AddRetry(options);
}) |
You can also use this right? return new ServiceCollection()
.Configure<RetryStrategyOptions>(configuration) // this binds values from configuration
.Configure<RetryStrategyOptions>(o =>
{
// just configure the callbacks here
o.ShouldRetry.HandleResult(-1).HandleException<InvalidOperationException>();
o.OnRetry.Register(() => Console.WriteLine("Retry occurred!"));
})
.AddResilienceStrategy("my-retry", context =>
{
// resolve options from DI
var options = context.ServiceProvider.GetRequiredService<IOptions<RetryStrategyOptions>>().Value;
context.Builder.AddRetry(options);
}) |
In my simple example yes, but I'd probably compose it up with a more structured configuration file based on other things relating to different downstream APIs etc, at which point there'd be different settings for different named strategies so it would end up getting into named options so I'd probably be binding things explicitly from options that are already bound onto the configuration. |
Won't be required after #1200. |
Details on the issue fix or feature implementation
Just creating this PR to discuss whether adding new extensions that accept lambdas that configure the options is something we want to have built-in. It simplifies the configuration of predicates, events and generators.
Adding retry strategy using the options:
Using the configure callback:
Note that the overload that accepts the options directly needs to stay as it will be API that can integrate with
IOptions
.Is the second API (in addition to the first one) something we want to have built-in?
Confirm the following