question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow Use of IConfigureOptions Consistently Everywhere

See original GitHub issue

Is your feature request related to a problem? Please describe.

IConfigureOptions<T> allows you to move your configuration code to a separate class. However, it cannot be used with certain API’s which require us to pass an Action<T>:

IServiceCollection AddStackExchangeRedisCache(this IServiceCollection services, Action<RedisCacheOptions> setupAction)
IServiceCollection AddHsts(this IServiceCollection services, Action<HstsOptions> configureOptions)

This is not an exhaustive list, there may be others.

Describe the solution you’d like

All API’s in ASP.NET Core should be consistent and allow us to configure them using IConfigureOptions<T>. The following overloads should be added:

IServiceCollection AddStackExchangeRedisCache(this IServiceCollection services)
IServiceCollection AddHsts(this IServiceCollection services)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
pranavkmcommented, Feb 14, 2022

API review:

  • Don’t change existing overloads.
  • For existing APIs that are missing the overload that does not accept an options callback, we should add one. For e.g we should do this:
+ IServiceCollection AddStackExchangeRedisCache(this IServiceCollection services)
IServiceCollection AddStackExchangeRedisCache(this IServiceCollection services, Action<RedisCacheOptions> setupAction)

  • For new ones, we think standardize on using the pattern of having two overloads. This should allow for new options to be introduced in a future release without deviating from the pattern and also conforms to existing patterns in the framework.
public static IServerSideBlazorBuilder AddMyFeature(this IServiceCollection services);
public static IServerSideBlazorBuilder AddMyFeature(this IServiceCollection services, Action<MyFeatureOption> configureOptions) ;

configureOptions in the above example can be allowed to be soft-allow null values (i.e. not perform null checks) for the sake of brevity of implementation.

  • In the event we only need the options type to configured without adding any other services, we should standardize on Configure??? as the name for the method e.g. ConfigureMyFeatureOptions(Action<MyFeatureOptions> configureOptions) where configureOptions is non-null.

Separately we’ll approve the changes proposed in this issue:

+ IServiceCollection AddStackExchangeRedisCache(this IServiceCollection services)
+ IServiceCollection AddHsts(this IServiceCollection services)
0reactions
halter73commented, May 16, 2022

API Review Notes:

Options can be configured separately without the callback, but this is uncommon. We’re worried that introducing this overload will create confusion only to save seven characters of code in the uncommon case.

We are worried that adding the non-options-configuring AddStackExchangeRedisCache(this IServiceCollection services) will create too much confusion by making people think configuring options isn’t necessary. If options are configured seperately, calling AddStackExchangeRedisCache(this IServiceCollection services, Action<RedisCacheOptions> configure) with _ => { } is easy enough.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The dangers and gotchas of using scoped services in ...
In this post I look at the problems you can hit with strongly-typed settings when you inject Scoped services into IConfigureOptions, ...
Read more >
How to configure options sometime later at runtime?
Reconfiguring options using IServiceCollection.Configure is not possible after initialization because BuildServiceProvider has already been ...
Read more >
Validated Strongly Typed IOptions - Kaylumah
The HostConfiguration is used to set variables like the DOTNET_ENVIRONMENT , which is used to load the proper appsettings.json and user secrets.
Read more >
Options pattern in ASP.NET Core
Discover how to use the options pattern to represent groups of related settings in ASP.NET Core apps.
Read more >
The Options pattern | An Atypical ASP.NET Core 6 Design ...
The Options pattern in ASP.NET Core allows us to seamlessly load settings from multiple sources. We can customize these sources when creating IHostBuilder...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found