gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:47:58 24 4
gpt4 key购买 nike

我正在尝试找到将 System.Text.Json 与 Azure Functions 结合使用的良好代码示例

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

例如,使用 Functions v4 进行 httpclient postasync 调用

最佳答案

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

  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>
  • 向同一项目添加了一个名为Entity的新类。其代码:
  • 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; }
    }
    }
    }
  • 相应地修改了 Http Trigger Function 类,以相应的 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 Request Call

    POST 请求调用: Post Request Call

    关于azure - 将 system.text.json 与 Azure Functions 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70900027/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com