Ogg*_*las 3 .net c# .net-core .net-5
正如标题所示,我创建了一个新的 .NET 5 WorkerService,但是当我调用它时,HostingEnvironment.IsDevelopment()即使环境变量ASPNETCORE_ENVIRONMENT设置为 ,它也会返回 false Development。
Program.cs 看起来像这样:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostContext, builder) =>
{
if (hostContext.HostingEnvironment.IsDevelopment())
{
builder.AddUserSecrets<Program>();
}
})
.ConfigureServices((hostContext, services) =>
{
var configuration = hostContext.Configuration;
var settings = new MyAppSettings();
configuration.GetSection("MyApp").Bind(settings);
services.AddSingleton(settings);
services.AddHttpClient();
services.AddHttpClient<MyAppHttpClient>();
services.AddHostedService<Worker>();
})
.ConfigureLogging(logging =>
{
logging.AddEventLog(eventLogSettings =>
{
eventLogSettings.SourceName = "MyAppBooker";
});
});
}
Run Code Online (Sandbox Code Playgroud)
环境变量在调试属性下设置,如下所示:
我可以确认环境变量也设置正确。
我正在遵循“开发中应用程序机密的安全存储”指南,代码来自那里。
我也尝试过设置NETCORE_ENVIRONMENT,但没有什么区别。
https://github.com/dotnet/aspnetcore/issues/4150
鉴于我认为的方法描述,它应该有效:
检查当前托管环境名称是否为 Development。
根据文档IHostingEnvironment.EnvironmentName Property应该这样做:
获取或设置环境的名称。主机自动将此属性设置为“ASPNETCORE_ENVIRONMENT”环境变量的值,或任何其他配置源中指定的“环境”。
鉴于此描述,我不明白为什么程序设置hostContext.HostingEnvironment.EnvironmentName为Production在调试中本地运行。
感谢@Jack A。缺少的链接是环境变量中的DOTNET_ENVIRONMENT值。Development发生错误是因为launchSettings.json不在 Git 中,因此从未设置原始环境变量。
| 归档时间: |
|
| 查看次数: |
2562 次 |
| 最近记录: |