Advertisement
Guest User

Untitled

a guest
Dec 10th, 2020
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         public static IHostBuilder CreateHostBuilder(string[] args) =>
  2.             Host.CreateDefaultBuilder(args)
  3.                 .ConfigureAppConfiguration(configurationBuilder =>
  4.                 {
  5.                     // nothing to do here
  6.                 })
  7.                 .ConfigureHostConfiguration(configurationBuilder =>
  8.                 {
  9.                     // nothing to do here
  10.                 })
  11.                 .ConfigureServices((hostBuilderContext, serviceCollection) =>
  12.                 {
  13.                     IConfigurationSection myConfigurationSection = hostBuilderContext.Configuration.GetSection("app");
  14.  
  15.                     serviceCollection.AddSingleton<IValidateOptions<AppOptions>, AppOptionsValidator>();
  16.                     serviceCollection.Configure<AppOptions>(myConfigurationSection);
  17.  
  18.                     // ! Just for testing purposes !
  19.  
  20.                     IServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
  21.                     IOptions<AppOptions> appOptions = serviceProvider.GetService<IOptions<AppOptions>>(); // appOptions threw a Microsoft.Extensions.Options.OptionsValidationException
  22.                     AppOptions x = appOptions.Value;
  23.                     string y = x.Foo;
  24.                 });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement