Skip to content

Instantly share code, notes, and snippets.

@configureappio
Created March 24, 2018 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save configureappio/9730b44a163f68f6aeb1e99020ea560b to your computer and use it in GitHub Desktop.
Save configureappio/9730b44a163f68f6aeb1e99020ea560b to your computer and use it in GitHub Desktop.
An example of using a lambda function to create a bridge between a settings class and IOptionsSnapshot<T>
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddOptions();
services.Configure<MyAppSettings>(Configuration.GetSection("MyAppSettings"));
// use a lambda to act as the bridge between requesting an instance of MyAppSettings but
// getting it from IOptionsSnapshot<MyAppSettings>
services.AddTransient<MyAppSettings>((opt) => opt.GetService<IOptionsSnapshot<MyAppSettings>>().Value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment