Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the IHostingEnvironment in static main of ASP.NET Core

I'm attempting to get configuration values in my static void main of my upgraded Asp.Net Core RC2 application. In the constructor for Startup, I can get IHostingEnvironment injected in, but can't do that in a static method. I'm following https://github.com/aspnet/KestrelHttpServer/blob/dev/samples/SampleApp/Startup.cs, but want to have my pfx password in appsettings (yes, it should be in user secrets and will get there eventually).

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();
            })
}
like image 788
AlignedDev Avatar asked May 26 '16 16:05

AlignedDev


People also ask

What is IHostingEnvironment in ASP.NET Core?

The IHostingEnvironment is an interface for . Net Core 2.0. The IHostingEnvironment interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IHostingEnvironment interface have two properties.

How do I get WebRootPath in .NET Core?

ContentRootPath – Path of the root folder which contains all the Application files. You will need to import the following namespace. In the below example, the IHostingEnvironment is injected in the Controller and assigned to the private property Environment and later used to get the WebRootPath and ContentRootPath.

How do I get IWebHostEnvironment in ConfigureServices?

You can easily access it in ConfigureServices, just persist it to a property during Startup method which is called first and gets it passed in, then you can access the property from ConfigureServices. public Startup(IWebHostEnvironment env, IApplicationEnvironment appEnv) { ... your code here...


1 Answers

After some discussion on aspnetcore.slack.com in the #general channel (May 26,2016 12:25pm), David Fowler said "you can new up the webhostbuilder and call getsetting(“ environment”)" and "hosting 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();
like image 87
AlignedDev Avatar answered Oct 02 '22 14:10

AlignedDev