如何配置 Azure Function v3 使用的默认 JsonSerializerOptions (System.Text.Json)?

How to configure a default JsonSerializerOptions (System.Text.Json) to be used by Azure Function v3?

我在 .net core 3.1 上有一套 Azure Functions v3 运行。

我有一个 JsonSerializerOptions 的自定义配置,我希望在 de/serializing 数据时由我的函数自动使用。

问题

如何设置我的 Azure Functions 以便它们可以使用我的默认 System.Text.Json.JsonSerializerOptions 实例?

更新 1

根据@sellotape 的建议,我找到了以下关于 JsonResult class 的文档:

问题是我的 JsonResult 实例没有这个 属性 类型的对象;它只接受一个 JsonSerializerSettings 实例。

更新 2

我仍然收到以下错误,我不确定 Newtonsoft 的来源:

Microsoft.AspNetCore.Mvc.NewtonsoftJson: Property 'JsonResult.SerializerSettings' must be an instance of type 'Newtonsoft.Json.JsonSerializerSettings'.

将我的 Azure Functions 从 v2 升级到 v3 时 I missed a step。为了让它工作,我必须将 Microsoft.AspNetCore.App 框架添加到我的 csproj。否则,我的项目一直引用 Microsoft.Aspnet.Mvc.Core v2.X.X 中的 JsonResult 不支持 System.Text.Json.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <!-- ADD THESE LINES -->
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

</Project>

然后,我可以像这样指定我自己的 JsonSerializerOptions 实例:

return new JsonResult(<ContractClass>, NSJsonSerializerOptions.Default)
{
    StatusCode = StatusCodes.Status200OK
};

其中 NSJsonSerializerOptions.DefaultJsonSerializerOptions 的静态实例。

对于那些拥有现有 Azure Function 应用程序的人来说,这不是一个完美的解决方案,但 Azure Functions for .net 5 isolated runtime 现在允许您配置 JSON 序列化选项。因此,对于开始新的 azure 函数或有预算和精力升级现有函数的任何人,现在 first-class 支持按照您喜欢的方式配置序列化程序。不幸的是,直到现在,枚举一直是第二位 class 公民,但迟到总比不到好。

Program.cs

using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace MyFunctionApp
{
    public class Program
    {
        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults()
                .ConfigureServices(services =>
                {
                    services.Configure<JsonSerializerOptions>(options => 
                    {
                        options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                        options.Converters.Add(new JsonStringEnumConverter());
                    });
                })
                .Build();

            host.Run();
        }
    }
}

如果您想选择 System.Text.Json 或 Newtonsoft.Json,您可以配置其中一个,例如 seen in this example

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.Core.Serialization;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace Configuration
{
    public class Program
    {
        public static void Main()
        {
            var host = new HostBuilder()
                .ConfigureFunctionsWorkerDefaults(workerApplication =>
                {
                    // Use any of the extension methods in WorkerConfigurationExtensions.
                })
                .Build();

            host.Run();
        }
    }

    internal static class WorkerConfigurationExtensions
    {
        /// <summary>
        /// Calling ConfigureFunctionsWorkerDefaults() configures the Functions Worker to use System.Text.Json for all JSON
        /// serialization and sets JsonSerializerOptions.PropertyNameCaseInsensitive = true;
        /// This method uses DI to modify the JsonSerializerOptions. Call /api/HttpFunction to see the changes.
        /// </summary>
        public static IFunctionsWorkerApplicationBuilder ConfigureSystemTextJson(this IFunctionsWorkerApplicationBuilder builder)
        {
            builder.Services.Configure<JsonSerializerOptions>(jsonSerializerOptions =>
            {
                jsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
                jsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
                jsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;

                // override the default value
                jsonSerializerOptions.PropertyNameCaseInsensitive = false;
            });

            return builder;
        }

        /// <summary>
        /// The functions worker uses the Azure SDK's ObjectSerializer to abstract away all JSON serialization. This allows you to
        /// swap out the default System.Text.Json implementation for the Newtonsoft.Json implementation.
        /// To do so, add the Microsoft.Azure.Core.NewtonsoftJson nuget package and then update the WorkerOptions.Serializer property.
        /// This method updates the Serializer to use Newtonsoft.Json. Call /api/HttpFunction to see the changes.
        /// </summary>
        public static IFunctionsWorkerApplicationBuilder UseNewtonsoftJson(this IFunctionsWorkerApplicationBuilder builder)
        {
            builder.Services.Configure<WorkerOptions>(workerOptions =>
            {
                var settings = NewtonsoftJsonObjectSerializer.CreateJsonSerializerSettings();
                settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                settings.NullValueHandling = NullValueHandling.Ignore;

                workerOptions.Serializer = new NewtonsoftJsonObjectSerializer(settings);
            });

            return builder;
        }
    }
}