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:
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:
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:
So how to set environment? Why Os variable doesn't work?