Access the IHostingEnvironment in the ASP.NET Static Core

I am trying to get configuration values โ€‹โ€‹in my static void main of my updated Asp.Net Core RC2 application. In the constructor for Startup, I can start IHostingEnvironment, but I can not do this in a static method. I follow https://github.com/aspnet/KestrelHttpServer/blob/dev/samples/SampleApp/Startup.cs , but I want to have my pfx password in the appsettings settings (yes, it must be in the user's secrets and will end up there )

public Startup(IHostingEnvironment env){} public static void Main(string[] args) { var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddJsonFile("hosting.json"); builder.AddEnvironmentVariables(); var configuration = builder.Build(); ... var host = new WebHostBuilder() .UseKestrel(options => { // options.ThreadCount = 4; options.NoDelay = true; options.UseHttps(testCertPath, configuration["pfxPassword"]); options.UseConnectionLogging(); }) } 
+5
source share
2 answers

If you want to save the password for the Https certificate, finally, in the User Secrets section, add the following lines to the appropriate sections in the Main of Program.cs section:

 var config = new ConfigurationBuilder() .AddUserSecrets("your-user-secrets-id") //usually in project.json var host = new WebHostBuilder() .UseConfiguration(config) .UseKestrel(options=> { options.UseHttps("certificate.pfx", config["your-user-secrets-id"]); }) 

User secrets must be passed directly, because the project.json configuration for "userSecretsId" is not yet available at this point.

+2
source

After some discussion on aspnetcore.slack.com on the #general channel (May 26,2016 12:25 pm), David Fowler said: โ€œYou can upgrade your web hosting and call getetting (" environment ")" and "host config"! = app config ".

 var h = new WebHostBuilder(); var environment = h.GetSetting("environment"); var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{environment}.json", optional: true) .AddEnvironmentVariables(); var configuration = builder.Build(); 
+16
source

Source: https://habr.com/ru/post/1266413/


All Articles