将自定义部分添加到 .NET Core local.settings.json
Add custom section to .NET Core local.settings.json
我正在编写我的第一个 Azure 函数(基于 .NET Core 2.x),并且我正在努力使用我的自定义映射扩展应用程序设置 table。
我有一个文件 local.settings.json
,我试图用 "custom" 配置部分扩展它,其中包含一些 "key/value" 对 - 像这样:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
... (standard config settings) ...
},
... (more default sections, like "Host" and "ConnectionStrings")
"MappingTable" : {
"01" : "Value1",
"02" : "Value2",
"03" : "Value3",
"else" : "Value4"
}
}
我通过构造函数注入将 IConfiguration
注入到我的工作程序 class 中,它对于存储在 "Values" 部分中的 "basic" 默认值工作得很好:
public MyWorker(IConfiguration config)
{
_config = config;
string runtime = _config["FUNCTIONS_WORKER_RUNTIME"]; // works just fine
// this also works fine - the "customSection" object is filled
var customSection = _config.GetSection("MappingTable");
// but now this doesn't return any values
var children = customSection.GetChildren();
// and trying to access like this also only returns "null"
var mapping = customSection["01"];
}
我被困在这里了 - 我发现的所有博客 post 和文章似乎都表明这样做 - 但就我而言,这似乎行不通。我在这里错过了什么?我非常熟悉完整的 .NET Framework 配置系统 - 但这对我来说是新的,而且还没有真正意义……
我也曾尝试将整个 MappingTable
部分移动到 appSettings.json
- 但这并没有改变任何东西,我在尝试访问时仍然只返回 null
我的自定义配置部分的值....
谢谢!
更新: 使用标准 ASP.NET Core 2.1 Web 应用程序可以很好地完成这项工作的所有建议方法 - 但在 Azure 功能中,它不起作用。似乎 Azure Function 中的代码 以不同方式处理配置 与常规 .NET Core 代码库 ...
我使用 .net core 3.0 做了类似的事情
local.settings.json
{
"AppSettings":{
"MappingTable" : {
"01" : "Value1"
}
}
}
阅读应用程序设置:
private void AppSettings()
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var 01 = config["AppSettings:MappingTable:01"];
}
在 Azure 门户中,您需要将其添加为应用程序设置。
在你的Function App -> Configuration -Application Settings -> New Application setting
Name:AppSettings:MappingTable:01
Value:Value1
A R G H ! ! !
我知道 - 如此愚蠢的小错误 - 但后果相当严重.....
在我的初创公司 class 中,我有这段代码(还有一些):
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
.....
}
}
但缺少的是在调用上面的 .Build()
之后的这一行代码:
builder.Services.AddSingleton<IConfiguration>(config);
不知何故,local.settings.json
的 Values
部分中的设置存在并可访问,但任何自定义配置部分都不存在。
添加这一行解决了我的问题 - 现在我可以轻松地从我的 local.settings.json
(或 appsettings.json
)文件中读取 MappingTable
并在我的代码中使用它。
我正在编写我的第一个 Azure 函数(基于 .NET Core 2.x),并且我正在努力使用我的自定义映射扩展应用程序设置 table。
我有一个文件 local.settings.json
,我试图用 "custom" 配置部分扩展它,其中包含一些 "key/value" 对 - 像这样:
{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
... (standard config settings) ...
},
... (more default sections, like "Host" and "ConnectionStrings")
"MappingTable" : {
"01" : "Value1",
"02" : "Value2",
"03" : "Value3",
"else" : "Value4"
}
}
我通过构造函数注入将 IConfiguration
注入到我的工作程序 class 中,它对于存储在 "Values" 部分中的 "basic" 默认值工作得很好:
public MyWorker(IConfiguration config)
{
_config = config;
string runtime = _config["FUNCTIONS_WORKER_RUNTIME"]; // works just fine
// this also works fine - the "customSection" object is filled
var customSection = _config.GetSection("MappingTable");
// but now this doesn't return any values
var children = customSection.GetChildren();
// and trying to access like this also only returns "null"
var mapping = customSection["01"];
}
我被困在这里了 - 我发现的所有博客 post 和文章似乎都表明这样做 - 但就我而言,这似乎行不通。我在这里错过了什么?我非常熟悉完整的 .NET Framework 配置系统 - 但这对我来说是新的,而且还没有真正意义……
我也曾尝试将整个 MappingTable
部分移动到 appSettings.json
- 但这并没有改变任何东西,我在尝试访问时仍然只返回 null
我的自定义配置部分的值....
谢谢!
更新: 使用标准 ASP.NET Core 2.1 Web 应用程序可以很好地完成这项工作的所有建议方法 - 但在 Azure 功能中,它不起作用。似乎 Azure Function 中的代码 以不同方式处理配置 与常规 .NET Core 代码库 ...
我使用 .net core 3.0 做了类似的事情
local.settings.json
{
"AppSettings":{
"MappingTable" : {
"01" : "Value1"
}
}
}
阅读应用程序设置:
private void AppSettings()
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
var 01 = config["AppSettings:MappingTable:01"];
}
在 Azure 门户中,您需要将其添加为应用程序设置。
在你的Function App -> Configuration -Application Settings -> New Application setting
Name:AppSettings:MappingTable:01
Value:Value1
A R G H ! ! !
我知道 - 如此愚蠢的小错误 - 但后果相当严重.....
在我的初创公司 class 中,我有这段代码(还有一些):
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddHttpClient();
var config = new ConfigurationBuilder()
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
.....
}
}
但缺少的是在调用上面的 .Build()
之后的这一行代码:
builder.Services.AddSingleton<IConfiguration>(config);
不知何故,local.settings.json
的 Values
部分中的设置存在并可访问,但任何自定义配置部分都不存在。
添加这一行解决了我的问题 - 现在我可以轻松地从我的 local.settings.json
(或 appsettings.json
)文件中读取 MappingTable
并在我的代码中使用它。