如何在 Azure Functions 中使用 IAsyncCollector 和 System.Text.Json 设置驼峰式大小写?

Art*_*ior 5 c# serialization azureservicebus azure-functions system.text.json

我正在将 Azure Functions v3 从 迁移Newtonsoft.JsonSystem.Text.Json并尝试让camelCase 在全球范围内工作。

对于这些:

  • 信号R
  • 服务总线输出绑定
  • 宇宙数据库

我能够在JsonSerializerOptions全局范围内显式传递或设置它(SignalR),但我无法为IAsyncCollector.

这是我的代码:

[FunctionName(nameof(SampleFunction))]
public async Task Run(
    [ServiceBusTrigger(ServiceBusQueue.QueueA)] string json,
    [ServiceBus(ServiceBusQueue.QueueB)] IAsyncCollector<Delivery> queueB,
    [ServiceBus(ServiceBusQueue.QueueC)] IAsyncCollector<Driver> queueC,
    ExecutionContext context)
{
        // ... do some work

        await queueB.AddAsync(objectB).ConfigureAwait(false);

        // ... do some more work

        await queueC.AddAsync(objectC).ConfigureAwait(false);
}
Run Code Online (Sandbox Code Playgroud)

objectB最终objectC乘坐服务巴士而不是骆驼箱。作为解决方法,我在接收函数上将属性名称设置为不区分大小写。

PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true
Run Code Online (Sandbox Code Playgroud)

知道如何使用IAsyncCollector驼峰案例进行序列化吗?

Ste*_*ary 4

IAsyncCollector我建议根本不要使用。提供者IAsyncCollector没有太多配置旋钮:

  • 序列化
  • 配料
  • 重试
  • 错误处理

更重要的是,某些实现在版本之间更改了这些实现细节,而没有发出警告。出于这些原因,我建议不要使用IAsyncCollectorAPI,而只是直接使用 API,这样您就可以完全控制所有这些方面。IAsyncCollector是一个很好的抽象,但正是这种抽象的本质使它最终变得不合适。