Skip to content

Instantly share code, notes, and snippets.

@dcagnetta
Last active August 15, 2022 08:15
Show Gist options
  • Save dcagnetta/480a8f2b0b9b1417e523bf9b3c101962 to your computer and use it in GitHub Desktop.
Save dcagnetta/480a8f2b0b9b1417e523bf9b3c101962 to your computer and use it in GitHub Desktop.
C# – How to supply IOptions
read configuration settings before initializing a Host in ASP .NET Core?
https://stackoverflow.com/questions/58530942/how-to-read-configuration-settings-before-initializing-a-host-in-asp-net-core#_=_
//rest of the class
public void ConfigureServices(IServiceCollection services)
{
//rest of method
services.AddOptions<MovieSettings>().Bind(Configuration.GetSection("MovieSettings"));
// Supply IOptions<T> with hardcoded values
services.AddSingleton<IOptions<MovieSettings>>(_ =>
{
return Options.Create(new MovieSettings()
{
MovieAPIUrl = "https://localhost:12345/movies/api"
});
});
// Supply IOptions<T> from a registered service
services.AddOptions<MovieSettings>()
.Configure<IMovieSettingsRepository>((movieSettings, movieSettingsRepo) =>
{
movieSettings.MovieAPIUrl = movieSettingsRepo.GetSettings().MovieAPIUrl;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment