Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 994
Mike Lunn Avatar asked Jun 08 '17 13:06

Mike Lunn


People also ask

How do I get environment variables in .NET Core?

Open project properties by right clicking on the project in the solution explorer and select Properties. This will open properties page. Click on Debug tab and you will see Environment Variables as shown below. You may change the value as per your need.

What is launchSettings JSON in .NET Core?

The launchSettings. json file is used to store the configuration information, which describes how to start the ASP.NET Core application, using Visual Studio. The file is used only during the development of the application using Visual Studio. It contains only those settings that required to run the application.


Video Answer


3 Answers

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;
like image 197
Henk Mollema Avatar answered Oct 11 '22 01:10

Henk Mollema


[New Answer using ASP 6.0 minimal API]:

If you are using ASP 6.0 minimal API it's very simple by using WebApplication.Environment:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

if (!app.Environment.IsProduction())
{
    // ...
}

app.MapGet("/", () => "Hello World!");

app.Run();

======================================

[Old Answer]

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();
}
like image 30
HamedH Avatar answered Oct 11 '22 01:10

HamedH


In .NET core 3.0

using System;
using Microsoft.Extensions.Hosting;

then

var isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == Environments.Development;
like image 8
Moses Machua Avatar answered Oct 10 '22 23:10

Moses Machua