ChatGPT解决这个技术问题 Extra ChatGPT

Access environment name in Program.Main in ASP.NET Core

Using ASP.NET Mvc Core I needed to set my development environment to use https, so I added the below to the Main method in Program.cs:

var host = new WebHostBuilder()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .UseKestrel(cfg => cfg.UseHttps("ssl-dev.pfx", "Password"))
                .UseUrls("https://localhost:5000")
                .UseApplicationInsights()
                .Build();
                host.Run();

How can I access the hosting environment here so that I can conditionally set the protocol/port number/certificate?

Ideally, I would just use the CLI to manipulate my hosting environment like so:

dotnet run --server.urls https://localhost:5000 --cert ssl-dev.pfx password

but there doesn't seem to be way to use a certificate from the command line.


P
Pang

I think the easiest solution is to read the value from the ASPNETCORE_ENVIRONMENT environment variable and compare it with Environments.Development:

var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var isDevelopment = environment == Environments.Development;

I forgot about about System.Environment. Thanks!
Note that in .NET Core 3.0+, the EnvironmentName class is marked as obsolete (docs.microsoft.com/en-us/dotnet/api/…), with the recommendation you switch to the Environments class (docs.microsoft.com/en-us/dotnet/api/…).
This only works when environment is set by the environment variable. It's also possible to set it from the command line with --environment "Development" for example
How would you do this if you're not using asp.net at all? Like if it's just a console app?
F
Felix K.

This is my solution (written for ASP.NET Core 2.1):

public static void Main(string[] args)
{
    var host = CreateWebHostBuilder(args).Build();

    using (var scope = host.Services.CreateScope())
    {
        var services = scope.ServiceProvider;
        var hostingEnvironment = services.GetService<IHostingEnvironment>();

        if (!hostingEnvironment.IsProduction())
           SeedData.Initialize();
    }

    host.Run();
}

The problem with this is it creates an entire instance of the application and destroys it again just to get the Environment name. Very heavy handed.
@steed given that this is a clean way to get the IHostingEnvironment and it is only done once before Run() I would say it's neglectable if it's a bit "heavy handed".
Need access to it before web host builder Build is caleld
@Steed util you don't call Run, as in CreateHostBuilder(args).Build().Run(), you aren't really creating the entire instance of the application, you just created the host, and requested an instance of IHostingEnvironment
IHostingEnvironment has been deprecated and can be replaced with Microsoft.AspNetCore.Hosting.IWebHostEnvironment or Microsoft.Extensions.Hosting.IHostEnvironment
M
Moses Machua

In .NET core 3.0

using System;
using Microsoft.Extensions.Hosting;

then

var isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development;