在预编译的 C# Azure 函数中使用命令式绑定时,如何定义 Cosmos DB 数据库和集合名称?

How do you define the Cosmos DB database and collection names when using the imperative bindings in a precompiled C# Azure Function?

在预编译的 C# Azure 函数中使用命令式绑定时,如何定义 Cosmos DB 数据库和集合名称?

我遵循了 run.csx C# 脚本示例 Store unstructured data using Azure Functions and Cosmos DB,它工作正常。

尝试对预编译的 C# 函数执行类似操作,发现生成的 function.json 缺少数据库和集合名称。

获得了 DocumentDB 属性的方法 ...

[HttpTrigger(
  AuthorizationLevel.Function, 
  "get", "post", 
  Route = null)] HttpRequestMessage req,
[DocumentDB(
  ConnectionStringSetting = "cflogdev_DOCUMENTDB", 
  CreateIfNotExists = true,
  PartitionKey = "user" )] out object logDocument,
ILogger log)

但是 Intellisense 没有数据库和集合作为可设置的值,但是它们确实有过载...

我检查了 DocumentDBAttribute 并将它们作为第二个构造函数的一部分。

但是如何设置它们呢?

DocumentDBAttribute(databaseName, collectionName) 构造函数在我使用的版本 (1.1.0-beta1) 中可用。这些属性是只读的,因此您不能从默认构造函数中设置它们。

这解决了...

public static HttpResponseMessage Run(
  [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequestMessage req,
  [DocumentDB(
        "logDatabase",
        "logCollection",
        ConnectionStringSetting = "cflogdev_DOCUMENTDB",
        //PartitionKey = "user",
        CreateIfNotExists = true
    )] out object logDocument,
  ILogger log)

我使用的是 Microsoft.Azure.WebJobs.Extensions.DocumentDB

的 1.0.0 版