Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为控制台应用程序设置ASPNETCORE_环境?_C#_.net Core_Asp.net Core Hosted Services_Aspnetcore Environment - Fatal编程技术网

C# 如何为控制台应用程序设置ASPNETCORE_环境?

C# 如何为控制台应用程序设置ASPNETCORE_环境?,c#,.net-core,asp.net-core-hosted-services,aspnetcore-environment,C#,.net Core,Asp.net Core Hosted Services,Aspnetcore Environment,我有以下包含托管服务的简单控制台应用程序: public static async Task Main(string[] args) { using (var host = Host.CreateDefaultBuilder(args) .ConfigureServices((hostContext, services) => { // db context

我有以下包含托管服务的简单控制台应用程序:

    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();
        }
    }
}
它试图从
生产环境开始。
请参阅调试输出:

那么如何设置环境呢?为什么Os变量不工作

根据.Net Core 3.0以后的版本,主机配置是从以DOTNET_前缀的环境变量(例如,DOTNET_环境)提供的

如果这不起作用,您还可以尝试在launchSettings.json中设置环境变量,就像在您的配置文件中这样

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

使用.NET Core 3,通用主机,而不是旧的
ASPNETCORE\uuu
前缀。

您使用的是.NET Core的哪个版本?@Shoejep我使用的是最后的3.1版本
"environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development",
    "DOTNET_ENVIRONMENT" : "Development"
}