因此,我的Azure函数在本地读取一组设置,并对每个对象执行一些逻辑。我的local.settings.json
在下面。
我可以在Portal设置中添加单数Settings
键,但是添加诸如projects
之类的数组的最佳方法是什么?我可以在项目中简单地包含另一个JSON文件吗?可能是愚蠢的问题,但到目前为止尚未找到答案。
{
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsSecretStorageType": "files",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"PersonalAccessToken": "..."
},
"Settings": {
"url": "https://dev.azure.com/myproject",
"genericProjectName": "myproject",
"genericWikiName": "myproject.wiki",
"projects": [
{
"parentPagePath": "/Release notes",
"name": "Project 1",
"wikiName": "Project-1.wiki",
"leasing": true
}
{
"parentPagePath": "/Release notes",
"name": "Project-2",
"wikiName": "Project-2.wiki",
"leasing": true
}
]
}
}
否,无法添加数组。原因是由于实现了将local.settings.json文件读取到环境变量中的源代码。具体实现如下:
public AppSettingsFile(string filePath)
{
_filePath = filePath;
try
{
var content = FileSystemHelpers.ReadAllTextFromFile(_filePath);
var appSettings = JsonConvert.DeserializeObject<AppSettingsFile>(content);
IsEncrypted = appSettings.IsEncrypted;
Values = appSettings.Values;
ConnectionStrings = appSettings.ConnectionStrings;
Host = appSettings.Host;
}
catch
{
Values = new Dictionary<string, string>();
ConnectionStrings = new Dictionary<string, string>();
IsEncrypted = true;
}
}
public bool IsEncrypted { get; set; }
public Dictionary<string, string> Values { get; set; } = new Dictionary<string, string>();
public Dictionary<string, string> ConnectionStrings { get; set; } = new Dictionary<string, string>();
您可以从设计开始就发现设置和连接字符串是目录类型。它不支持数组。
因此,您有两种实现目标的方法。
First way,更改结构。
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsSecretStorageType": "files",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"PersonalAccessToken": "...",
"projects1-parentPagePath": "/Release notes",
"projects1-name": "Project 1",
"projects1-wikiName": "Project-1.wiki",
"projects1-leasing": true,
"projects2-parentPagePath": "/Release notes",
"projects2-name": "Project-2",
"projects2-wikiName": "Project-2.wiki",
"projects2-leasing": true
}
第二种方式,设计自己的代码。
您可以创建自己的json文件并填写所需的代码。然后将其属性中的copy属性更改为要复制的属性。
然后您可以设计自己的代码以读取json文件的信息。这是一个简单的示例:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Text;
namespace HttpTrigger
{
public static class Function1
{
public static string GetFileJson(string filepath)
{
string json = string.Empty;
using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8")))
{
json = sr.ReadToEnd().ToString();
}
}
return json;
}
//Read Json Value
public static string ReadJson()
{
string jsonfile = "custom.json";
string jsonText = GetFileJson(jsonfile);
JObject jsonObj = JObject.Parse(jsonText);
string value = ((JObject)jsonObj["Settings"])["projects"]["parentPagePath"].ToString();
return value;
}
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
string value = ReadJson();
log.LogInformation("C# HTTP trigger function processed a request.");
string name = req.Query["name"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($"Hello, {name}")
: new BadRequestObjectResult("Please pass a name on the query string or in the request body" + value);
}
}
}