gpt4 book ai didi

azure - Azure 应用服务中的 .NET Core WebJob 配置

转载 作者:行者123 更新时间:2023-12-02 08:03:44 24 4
gpt4 key购买 nike

我已将 Web 作业编写为 .NET Core 控制台应用程序 (exe),其中包含 appsettings.json。

如何在 Azure 中配置 WebJob?基本上,我想与 Web 应用程序共享一些设置,例如连接字符串,这些设置是通过应用程序服务的应用程序设置进行配置的。

最佳答案

从 ASP.NET Core 获取这些设置的方法是访问注入(inject)的环境变量。

因此,我们必须将这些环境变量加载到 Startup.cs 文件中的配置中:

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

appsettings.json 文件的示例如下:

enter image description here

如果您想获取 appsettings.json 文件中定义的名为“Redis”的连接字符串,我们可以通过配置获取它:

配置["ConnectionStrings:Redis"]

您可以在 Azure 门户上的 web 应用程序的 Appsettings 中设置此配置:

enter image description here

我们还可以使用 Configuration.GetConnectionString("Redis") 从 appsettings.json 文件中获取开发连接字符串,并在 Web 应用程序的“连接字符串”面板中设置不同的字符串来覆盖它当应用程序在 Azure 中部署并运行时。

更详细的可以引用这个article .

关于azure - Azure 应用服务中的 .NET Core WebJob 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49256615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com