0
votes

I am having problems with running my dotnet core 2.0 API (on Ubuntu 16.04 using .NET Core CLI) using the correct Environment settings.

I added into the "~/.bash_profile" the following:

ASPNETCORE_ENVIRONMENT=staging

When i run the project (using dotnet run) it seems to work correctly, It detects that it should be on staging. However, when i compile using "dotnet publish" and try to run it using:

nohup dotnet API.dll --urls "http://*:1337" &> /var/log/dotnetlogs

it does not seem to detect the environment variable and always defaults to using the "Production" environment.

Startup.cs:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

Program.cs

public static void Main(string[] args)
{

    var config = new ConfigurationBuilder()
        .AddCommandLine(args)
        .Build();
    var host = new WebHostBuilder()
        .UseKestrel()
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseConfiguration(config)
        .UseStartup<Startup>()
        .Build();

    host.Run();
}

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .Build();

Any idea how i can get this working?

1
Make sure your environment variable isn't getting overwritten in launchSettings.jsonIsaac Kleinman
Hi, thanks for the suggestion. However i believe that launchSettings.json is generated by Visual Studio, and the .NET Core CLI does not create this file when building/publishing. And in fact, i can not find this file anywhere on the ubuntu machine.Bardi

1 Answers

0
votes

Are you missing the export? The docs also say that Linux is case-sensitive, so you'll probably want to use Staging.

export ASPNETCORE_ENVIRONMENT=Staging