Azure App Service中的.NET Core WebJob配置

编程入门 行业动态 更新时间:2024-10-19 15:44:13
本文介绍了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配置

本文发布于:2023-11-14 04:23:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Service   App   Azure   WebJob   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!