Azure Function Cosmos DB 输出绑定(bind) - 自定义 JsonSerializerSettings

标签 azure json.net azure-functions azure-cosmosdb azure-functions-core-tools

我有一个带有 CosmosDB 输出绑定(bind)的 Azure 函数,如下所示:

public static class ComponentDesignHttpTrigger
{
    [FunctionName("ComponentDesignInserter-Http-From-ComponentDesign")]
    public static IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "fromComponentDesign")] HttpRequest request,
        [CosmosDB(
            databaseName: StorageFramework.CosmosDb.DatabaseId,
            collectionName: Storage.ComponentDesignCollectionId,
            ConnectionStringSetting = "CosmosDBConnection")] out ComponentDesign componentDesignToInsert,
        ILogger log)
    {
        var requestBody = new StreamReader(request.Body).ReadToEnd();
        componentDesignToInsert = JsonConvert.DeserializeObject<ComponentDesign>(requestBody);

        return new OkObjectResult(componentDesignToInsert);
    }
}

在此函数中,componentDesignToInsert 会在函数执行完成后自动序列化并放入 CosmosDB 中。但默认的序列化不会将东西放在驼峰命名法中。为此,Json.NET 允许您提供自定义序列化器设置,如下所示:

var settings = new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

var json = JsonConvert.SerializeObject(yourObject, settings);

但我不确定如何将其与我的输出绑定(bind)集成。我怎样才能做到这一点?

最佳答案

输出绑定(bind)此时不会公开序列化器设置。

您可以做的一件事是利用您自己的自定义 DocumentClient 进行操作。

但有一点很重要,DocumentClient 实例需要是静态的(有关 https://github.com/Azure/azure-functions-host/wiki/Managing-Connections 的更多详细信息)。

private static Lazy<DocumentClient> lazyClient = new Lazy<DocumentClient>(InitializeDocumentClient);
private static DocumentClient documentClient => lazyClient.Value;

private static DocumentClient InitializeDocumentClient()
{
    // Perform any initialization here
    var uri = new Uri("example");
    var authKey = "authKey";

    var settings = new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver()
    };
    return new DocumentClient(uri, authKey, settings);
}

[FunctionName("ComponentDesignInserter-Http-From-ComponentDesign")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "fromComponentDesign")] HttpRequest request,
    ILogger log)
{
    var requestBody = new StreamReader(request.Body).ReadToEnd();
    var componentDesignToInsert = JsonConvert.DeserializeObject<ComponentDesign>(requestBody);

    var collectionUri = UriFactory.GetDocumentCollectionUri(StorageFramework.CosmosDb.DatabaseId, Storage.ComponentDesignCollectionId);
    await documentClient.UpsertDocumentAsync(collectionUri, componentDesignToInsert);
    return new OkObjectResult(componentDesignToInsert);
}

如果适合您的场景,另一种选择是使用 JsonProperty 修饰类。

关于Azure Function Cosmos DB 输出绑定(bind) - 自定义 JsonSerializerSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55056533/

相关文章:

azure - AZ复制开发到生产

powershell - Azure网站资源模板错误

c# - 实现自定义 JsonWriter (JSON.net)

azure - 使用 DocumentDB(客户端库)在本地测试 Azure Functions,并针对 1.13.2 到 1.17.0 接收 'Invalid API version'

powershell - Azure 函数和 Powershell - 无法使用证书登录 AzureRmAccount

azure - 为什么 Azure 云服务上的更改配置需要这么长时间?

Azure IP 地址拒绝 Docker

c# - 如何强制 C# NewtonSoft 反序列化器使用已经创建的对象进行反序列化

c# - 序列化我的对象时如何加密选定的属性?

azure - 在不使用 SendGrid 和 Office 365 的情况下从 Azure Function App 发送电子邮件