Azure App Service中的.NET Core WebJob配置 [英] .NET Core WebJob configuration in Azure App Service

查看:111
本文介绍了Azure App Service中的.NET Core WebJob配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将Web作业编写为.NET Core控制台应用程序(exe),该应用程序具有appsettings.json.

I've written web job as .NET Core Console Application (exe), that has appsettings.json.

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

How do I configure the WebJob in Azure? Basically I want to share some settings like connection string with the web app, that is configured trough App Service's Application Settings.

推荐答案

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

The way to get these settings from our ASP.NET Core is accessing to the injected environment variables.

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

Hence we have to load these environment variables into our Configuration in the Startup.cs file:

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文件的示例为:

An example of appsettings.json file would be:

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

If you want to get the connection string named "Redis" defined in the appsettings.json file we could get it through our Configuration:

Configuration["ConnectionStrings:Redis"].

您可以在Azure门户上的webapp的Appsettings中设置此配置:

You could set this Configuration in Appsettings in webapp on azure portal:

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

Also we can use Configuration.GetConnectionString("Redis") to get a development connection string from our appsettings.json file and override it setting a different one in the Connection String panel of our Web App when the application is deployed and running in Azure.

有关更多详细信息,您可以参考此

For more detail, you could refer to this article.

这篇关于Azure App Service中的.NET Core WebJob配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆