Azure functions Azure功能-添加设置数组?

Azure functions Azure功能-添加设置数组?,azure-functions,settings,Azure Functions,Settings,因此,我的Azure函数在本地读取一组设置,并对每个对象执行一些逻辑。 下面是我的local.settings.json 我可以在门户设置中添加单个设置键,但是添加项目等数组的最佳方法是什么?我可以简单地在我的项目中包含另一个JSON文件吗?可能是个愚蠢的问题,但到目前为止还没有找到答案 { "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "AzureWebJobsSecretStorageTy

因此,我的Azure函数在本地读取一组设置,并对每个对象执行一些逻辑。 下面是我的
local.settings.json

我可以在门户设置中添加单个
设置
键,但是添加
项目等数组的最佳方法是什么?我可以简单地在我的项目中包含另一个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>();
第二种方式,设计自己的代码

您可以创建自己的json文件并填写所需的代码。然后将其属性中的copy属性更改为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);
        }
    }
}
使用系统;
使用System.IO;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用系统文本;
命名空间HttpTrigger
{
公共静态类函数1
{
公共静态字符串GetFileJson(字符串文件路径)
{
string json=string.Empty;
使用(FileStream fs=newfilestream(filepath,FileMode.Open,System.IO.FileAccess.Read,FileShare.ReadWrite))
{
使用(StreamReader sr=newstreamreader(fs,Encoding.GetEncoding(“utf-8”))
{
json=sr.ReadToEnd().ToString();
}
}
返回json;
}
//读取Json值
公共静态字符串ReadJson()
{
字符串jsonfile=“custom.json”;
字符串jsonText=GetFileJson(jsonfile);
JObject jsonObj=JObject.Parse(jsonText);
字符串值=((JObject)jsonObj[“设置”])[“项目”][“父页面路径”]。ToString();
返回值;
}
[功能名称(“功能1”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,“post”,Route=null)]HttpRequest请求,
ILogger日志)
{
字符串值=ReadJson();
LogInformation(“C#HTTP触发器函数处理了一个请求。”);
字符串名称=请求查询[“名称”];
string requestBody=等待新的StreamReader(req.Body).ReadToEndAsync();
动态数据=JsonConvert.DeserializeObject(requestBody);
名称=名称??数据?.name;
返回名称!=null
?(ActionResult)新的OkObjectResult($“你好,{name}”)
:new BadRequestObjectResult(“请在查询字符串或请求正文中传递名称”+值);
}
}
}

不,这是不可能的。这是经过设计的。但是,您可以将自己的json文件部署到Azure,然后自己处理。你可以看看我写的答案。谢谢,我会很快尝试,并在我找到答案后接受你的答案。:)
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);
        }
    }
}