如何在 azure 函数中访问 http 请求的所有字段(在 C# 中解析 JSON)?

How I can access all fields of a http-request in an azure function (parse JSON in C#)?

Microsoft Azure 对我来说是全新的编程主题。 编程基础语言是C#。 我必须使用逻辑应用程序中的 Azure Funtion Http 触发器(当新电子邮件到达时)。 我将日志应用程序中收到的电子邮件中的所有可能数据提供给 到 azure 函数调用。

然后我首先遇到了问题,HttpRequest 请求没有任何内容(没有数据)。

我发现 C# .NetCore3.1 对 JSON-Object 有问题并取回空对象。 然后我用 .NetCore2.1 构建项目并且它通常工作。 我得到了正确的信息,例如 req.Host,并且在这样的调用后我在数据中看到了一个有效的 JSON-对象

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);

但我不知道如何解析来自 JSON-object 的数据,例如(从、到、主题、内容、附件等)。

我已经尝试了很多我找到的样本,但我不喜欢什么有效,例如获取 null,异常,..

可以在这里取悦某人 post 一个适用于使用 VS2017/2019 .NetCore2.1..

编译的 azure 函数的小代码片段

另外一个问题是最终有没有办法直接从 http 请求中获取 MimeMessage-obj? 然后我可以直接从该对象获取信息,但可以帮助我解决这两种解决方案中的一种 哪里好

否则我会在自己身上拆分 JSoN 结构,以一种非常尴尬的方式获取信息。 所以我希望有人能帮助我。

        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
        log.LogInformation($" host   : {req.Host}");

        string name = req.Query["name"];
        // read the contents of the posted data into a string
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();

        // use Json.NET to deserialize the posted JSON into a C# dynamic object
        dynamic data = JsonConvert.DeserializeObject(requestBody);

        name = name ?? data?.name;

例如,我有这样一个 Json-content(在这两行代码之后的 dtaa 中):

string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); 动态数据 = JsonConvert.DeserializeObject(requestBody);

Json-要解析的简短电子邮件的内容(其他请求可能在更多或更少的参数(字段)方面有所不同)

{ {
        "Conversion Id": "AAQkADRmZDAwMTMwLTI3MTMtNGI0Ny1iMzFiLTQzYWJiZDY0YWI1ZQAQAE2OZM06t0uQqpd9MuONKNQ=",
        "From": "test1@testxyz.com",
        "Has Attachment": false,
        "Internet Message Id": "<AM0PR07MB44178000A0B37882D6BC01D99ACC0@AM0PR07MB4417.eurprd07.prod.outlook.com>",
        "Is Html": false,
        "Is rRad": false,
        "Message Id": "AAMkADRmZDAwMTMwLTI3MTMtNGI0Ny1iMzFiLTQzYWJiZDY0YWI1ZQBGAAAAAAAvlJZPJxnGS4f6YWxh7zGsBwBnLziSnuG8R6h5C2SVmlYlAAHViSWpAACAWl3JfUo4SI7D5g-MgfEiAAJiPJQeAAA=",
        "Received Time": "2020-12-09T14:05:06+00:00",
        "Subject": "test1",
        "To": "test2@testxyz.com",
        "emailBody": "1234\r\n",
        "importance": "normal"
    }
}

对于这个需求,你需要在你的函数代码中创建一个内部class来将请求体解析为一个对象。请参考我下面的代码:

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;

namespace FunctionApp2
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            //dynamic data = JsonConvert.DeserializeObject(requestBody);
            EmailItem data = JsonConvert.DeserializeObject<EmailItem>(requestBody);

            log.LogInformation(data.From);
            log.LogInformation(data.Has_Attachment.ToString());

            return new OkObjectResult("completed");
        }
    }

    public class EmailItem
    {
        public string From { get; set; }

        public Boolean Has_Attachment { get; set; }

        //Add other fields which you want, please note add all of the fields which your request may contains in this inner class.
    }
}

正如您提到的 other request could differn in more ore lesser paramters(fields),因此请在 EmailItem class.

中添加您的请求可能包含的所有字段

顺便说一句,我上面提供的代码是根据您的要求使用.net core 2.1 进行测试的。但我认为代码也可以在 .net core 3.1