【问题标题】:Azure Function Cosmos DB Output Binding - Custom JsonSerializerSettingsAzure 函数 Cosmos DB 输出绑定 - 自定义 JsonSerializerSettings
【发布时间】:2019-03-08 04:01:33
【问题描述】:

我有一个带有 CosmosDB 输出绑定的 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。但是默认的序列化并没有把东西放在camelCase中。对于此 Json.NET,您可以提供自定义序列化程序设置,如下所示:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    此时输出绑定不公开序列化程序设置。

    您可以做的一件事是利用您自己的自定义 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 装饰班级。

    【讨论】:

    • 这是否会因为我没有使用绑定而产生更多的计算周期?
    • 没有。绑定做了类似的事情,它在下面维护一个静态 DocumentClient,它只是不公开它以使其更加用户友好。但是绑定没有完全自定义。
    • 那么,如果我有很多类使用这种模式和相同的连接参数,这会在后台缓存或共享吗?还是我有很多连接,每个都连接到同一个数据库?
    猜你喜欢
    • 2021-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    相关资源
    最近更新 更多