【问题标题】:How to port local.settings.json from azure function to .NET core web project?如何将 local.settings.json 从 azure 函数移植到 .NET 核心 Web 项目?
【发布时间】:2020-04-20 20:18:56
【问题描述】:

我有点困惑如何处理 Web 项目中的本地设置,对于我们的 Azure 功能,这似乎非常简单,我们使用 local.settings.json 然后我们使用 ConfigurationBuilder 如下处理本地与 Azure 配置设置

private static readonly IConfiguration config = new ConfigurationBuilder()
        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
        .AddEnvironmentVariables()
        .Build();

private static readonly string cosmosDBEndpointUrl = config["cosmosDBEndpointUrl"];
private static readonly string cosmosDBPrimaryKey = config["cosmosDBPrimaryKey"];
private static readonly string cosmosDBName = config["cosmosDBName"];
private static readonly string cosmosDBCollectionNameRawData = config["cosmosDBCollectionNameRawData"];

这适用于我们的函数,因此它可以读取本地 dev 的本地设置和我们发布到的任何 env 的 azure 设置。

但是在我的 webproject 中没有 local.settings.json 那么我该如何处理那里的相同场景呢?

我查看了 appsettings.json,但格式似乎不同。

【问题讨论】:

  • 据我所知,在 ASP.NET Core Web 应用中,我们可以将应用设置保存在 appsettings.json 中(如{ "MyConfig": { "ApplicationName": "MyApp", "Version": "1.0.0" } })。关于如何阅读,请参考stackoverflow.com/questions/31453495/…
  • 在 .Net Framework web 应用中,我们可以在 web.config 中使用 元素为 保存应用设置(例如<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="countoffiles" value="7" /> <add key="logfilelocation" value="abc.txt" /> </appSettings> </configuration>)。关于如何阅读,我们可以使用System.Configuration.ConfigurationManager.AppSettings["<key name>"];

标签: c# azure-functions azure-web-app-service


【解决方案1】:

两者几乎相同。您可以随心所欲地拥有任何myFile.config.json。您需要做的就是从您的 Startup 类中注入它。

var config = new ConfigurationBuilder()
     ..SetBasePath(Environment.CurrentDirectory)
     .AddJsonFile("app.settings.json", optional: true, reloadOnChange: true)
     .AddEnvironmentVariables()
     .Build();

要访问文件中的值,您可以使用IConfiguration 类。 例如:

public MyService(IConfiguration configuration){
  var emailConfig = configuration.GetSection("Email");
  var userName = emailConfig["Username"];
}

更好的方法是让.json 文件基于环境。

【讨论】:

  • 但是当我部署到 Azure 时,这会自动从 Azure 应用程序设置中读取吗?看起来不像
  • 您是在问这会自动在 appsettings 中部署 json 值吗?
  • 不,我的意思是当我使用 azure 函数时,它只会在本地读取文件,文件永远不会部署,并且在 azure 中它会自动读取应用程序设置,这会以同样的方式工作吗?
  • 是的,它会以同样的方式工作,因为这行 AddEnvironmentVariables() 会将设置发布到云端
【解决方案2】:

正如 HariHaran 所说,在 ASP.NET Core Web 应用中,我们可以将应用设置保存在本地的 appsettings.json(如{ "MyConfig": { "ApplicationName": "MyApp", "Version": "1.0.0" } })中,我们可以使用以下代码:

// in Stratup
var config = new ConfigurationBuilder()
     ..SetBasePath(Environment.CurrentDirectory)
     .AddJsonFile("app.settings.json", optional: true, reloadOnChange: true)
     .AddEnvironmentVariables()
     .Build();
in other class
public MyService(IConfiguration configuration){
  var emailConfig = configuration.GetSection("Email");
  var userName = emailConfig["Username"];
}

当我们将 Web 应用部署到 Azure 时,我们可以直接将这些设置和连接字符串保存在 Application settings 中。然后我们可以将它们读取为环境变量。

include Microsoft.Extensions.Configuration;

namespace SomeNamespace 
{
    public class SomeClass
    {
        private IConfiguration _configuration;

        public SomeClass(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public SomeMethod()
        {
            // retrieve App Service app setting
            var myAppSetting = _configuration["MySetting"];
            // retrieve App Service connection string
            var myConnString = _configuration.GetConnectionString("MyDbConnection");
        }
    }
}

【讨论】:

    猜你喜欢
    • 2016-04-10
    • 2022-11-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 2020-09-07
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    相关资源
    最近更新 更多