将 system.text.json 与 Azure Functions 结合使用

Using system.text.json with Azure Functions

我正在尝试寻找将 System.Text.Json 与 Azure Functions

结合使用的优秀代码示例

我知道它仍然是一个相当新的包,但它的性能优势非常引人注目,我找不到的是将它导入 Azure 函数并执行基本 JSON 序列化和反序列化调用的简单示例.

例如,使用 Functions v4 的 httpclient postasync 调用

我对基本 JSON Azure Functions v4 Http 请求中的基本 JSON 序列化和反序列化调用使用异步运算符使用 Get 和 Post 方法进行序列化和反序列化的解决方法之一:

  1. 在 Visual Studio 中创建了 Stack .Net Core 6 的 Azure Functions v4。 这是 .csproj 代码:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.0.1" />
    <!--<PackageReference Include="Polly" Version="7.2.3" />
    <PackageReference Include="Polly.Extensions.Http" Version="3.0.0" />-->
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>
  1. 在同一项目中添加了名为 Entity 的新 class。它的代码:
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KrishAzFuncNet6Http
{
    internal class Entity
    {
        public enum ProductType
        {
            Book,
            Album,
            Cinema
        }

        public class Product
        {
            [JsonPropertyName("id")]
            public Guid Id { get; set; }

            [JsonPropertyName("type")]
            public ProductType Type { get; set; }

            [JsonPropertyName("is_ready")]
            public bool IsReady { get; set; }

            [JsonPropertyName("name")]
            public string Name { get; set; }

            [JsonPropertyName("revision")]
            public int Revision { get; set; }

            [JsonPropertyName("authors")]
            public IList<string> Authors { get; set; }

            [JsonPropertyName("registed_at")]
            public DateTime RegistedAt { get; set; }

            [JsonPropertyName("updated_at")]
            public DateTime UpdatedAt { get; set; }
            }
        }
    }
  1. 相应地修改了 Http 触发器函数 class 以相应的 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 System.Collections.Generic;
using static KrishAzFuncNet6Http.Entity;
using System.Text.Json;


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

            var product = new Product
            {
                Id = Guid.NewGuid(),
                Type = ProductType.Cinema,
                IsReady = false,
                Name = "Hari",
                Revision = 1,
                Authors = new List<string> { "Krishna", "Rajoli" },
                RegistedAt = DateTime.UtcNow,
                UpdatedAt = DateTime.UtcNow
            };
            var json = System.Text.Json.JsonSerializer.Serialize(product);

            string responseMessage = json;
            return new OkObjectResult(responseMessage);

        }
    }
}

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet"
  }
}

GET 请求:

POST 请求调用: