13

I have the following simple console application that contains a hosted service:

    public static async Task Main(string[] args)
    {
        using (var host = Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                // db context
                services.AddEntityFrameworkNpgsql()
                    .AddDbContext<ApplicationDbContext>();

                // hosted services
                services.AddHostedService<ConsumeScopedServiceHostedService>();
                services.AddScoped<ProcessManuallySendings>();

                // services
                services.AddHttpClient<ISendPushService, SendPushService>(x
                    =>
                {
                    x.Timeout = TimeSpan.FromSeconds(65);
                });
            })
            .Build())
        {
            // Start the host
            await host.StartAsync();

            // Wait for the host to shutdown
            await host.WaitForShutdownAsync();
        }
    }
}

It works with my database and it means that it requires connection string. I have three appsettings.json files:

enter image description here

On the development server I will use Development environment, on the production - Production. On my local machine I'm going to use Local. It's simple. I'm going to get it with the help of ASPNETCORE_ENVIRONMENT (OS environment variable).

I use Linux and in my shell (zsh) config file I have:

enter image description here

When I type in my terminal $ echo $ASPNETCORE_ENVIRONMENT I have Local. But when I start my console application

$ dotnet run // in the project folder

It's trying to start with Production environment. See debug output:

enter image description here

So how to set environment? Why Os variable doesn't work?

2
  • What version of .net core are you using?
    – Shoejep
    Commented Feb 13, 2020 at 11:33
  • @Shoejep I'm using the last 3.1 Commented Feb 13, 2020 at 11:54

3 Answers 3

21

According to the docs for .Net Core 3.0 onwards, the host configuration is provided from environment variables prefixed with DOTNET_ (for example, DOTNET_ENVIRONMENT).

If that doesn't work, you could also try setting the environment variable in the launchSettings.json like this on your profile.

"environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "DOTNET_ENVIRONMENT" : "Development"
}
0
15

With .NET Core 3, the generic host uses the DOTNET_ prefix, not the old ASPNETCORE_ prefix.

0
1

When setting environment variables in rider for a console application in dotnet core 3.1, neither ASPNETCORE_ENVIRONMENT nor DOTNET_ENVIRONMENT worked regardless of whether they were set in the Run/Debug Configuration or LaunchSettings.

In my situation, I am using Microsoft.Extensions.Hosting and Microsoft.Extensions.Hosting.Abstractions and wanted hostEnvironment to be resolved correctly to Development.

if (hostEnvironment.IsDevelopment())
{
    optionsBuilder.EnableSensitiveDataLogging();
    optionsBuilder.AddInterceptors(new EfCommandInterceptor());
}

To get this working, I had to create an environment variable called ENVIRONMENT with the appropriate value.

ENVIRONMENT=Development

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.