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

Kzr*_*tof 8 .net c# azure azure-functions system.text.json

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

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

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

更新 1

根据@sellotape 的建议,我找到了有关该JsonResult课程的以下文档:

JsonResult 文档

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

更新 2

我仍然收到以下错误,我不确定 Newtonsoft 来自哪里:

Microsoft.AspNetCore.Mvc.NewtonsoftJson: Property 'JsonResult.SerializerSettings' must be an instance of type 'Newtonsoft.Json.JsonSerializerSettings'.
Run Code Online (Sandbox Code Playgroud)

小智 17

对于那些拥有现有 Azure Function 应用程序的用户来说,这并不是一个完美的解决方案,但Azure Functions for .net 5 隔离运行时允许你现在配置 JSON 序列化选项。因此,对于任何开始新的 Azure 功能或有预算和精力升级现有功能的人来说,现在可以按照您喜欢的方式配置序列化器,提供一流的支持。不幸的是,到目前为止,枚举一直是二等公民,但迟到总比不到好。

程序.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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您想在那里选择 System.Text.Json 或 Newtonsoft.Json,您可以像本示例中所示配置其中之一

// 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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 很好的例子,非常有帮助,对我在 .net 6/functions v4 上有用 (2认同)

Kzr*_*tof 3

事实证明,我在将 Azure Functions 从 v2 升级到 v3 时错过了一个步骤。为了使其工作,我必须将Microsoft.AspNetCore.App框架添加到我的csproj. 否则,我的项目一直引用JsonResultv2.XX Microsoft.Aspnet.Mvc.Core,它不支持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>
Run Code Online (Sandbox Code Playgroud)

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

return new JsonResult(<ContractClass>, NSJsonSerializerOptions.Default)
{
    StatusCode = StatusCodes.Status200OK
};
Run Code Online (Sandbox Code Playgroud)

其中NSJsonSerializerOptions.Default是 的静态实例JsonSerializerOptions