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

How to set camel case using IAsyncCollector with System.Text.Json in Azure Functions?

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

对于这些:

我能够显式传递 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);
}

objectBobjectC 最终出现在服务总线中,而不是驼峰式大小写。作为解决方法,我在接收函数中将 属性 名称设置为不区分大小写。

PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
PropertyNameCaseInsensitive = true

知道如何让 IAsyncCollector 以驼峰式大小写连载吗?

我建议完全不要使用 IAsyncCollectorIAsyncCollector 的供应商没有很多配置旋钮:

  • 序列化
  • 批处理
  • 重试
  • 错误处理

更重要的是,一些实现在发布之间 更改了 这些实现细节,但没有发出警告。出于这些原因,我建议永远不要使用 IAsyncCollector,而是直接使用 API,您可以完全控制所有这些方面。 IAsyncCollector 是一个很好的抽象,但恰恰是那种抽象的性质使它最终不适合。