Last active
August 15, 2022 08:15
-
-
Save dcagnetta/480a8f2b0b9b1417e523bf9b3c101962 to your computer and use it in GitHub Desktop.
C# – How to supply IOptions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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#_=_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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