c# - 从 Azure Function 内部添加到 Azure CosmosDB

标签 c# azure azure-cosmosdb azure-functions

我创建了一个 CosmosDB 数据库,其中包含一个名为 MyTable 的表。我的想法是我想从 Azure 函数插入到该表中。环顾四周后,我想出了这个功能:

public static HttpResponseMessage Run(HttpRequestMessage req, TraceWriter log, out object tableOutput)
{
    log.Info("C# HTTP trigger function processed a request.");

    // parse query parameter
    string field1 = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "field1", true) == 0)
        .Value;

    string field2 = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "field2", true) == 0)
        .Value;


    var item = new 
    {    
        field1 = field1,
        field2 = field2
    };

    tableOutput = item;

    return req.CreateResponse(HttpStatusCode.OK);
}

我得到的错误是这样的:

2017-12-07T15:52:44.066 Exception while executing function: Functions.MyFunc. Microsoft.Azure.WebJobs.Host: Error while handling parameter tableOutput after function returned:. Microsoft.Azure.WebJobs.Extensions.DocumentDB: The collection 'tableOutput' (in database 'myCosmosDB') does not exist. To automatically create the collection, set 'CreateIfNotExists' to 'true'. Microsoft.Azure.Documents.Client: Message: {"Errors":["Owner resource does not exist"]}

我已经设置了输出参数,并且我看到了这里提到的复选框(CreateIfNotExists);然而,我有一个现有的 Cosmos 表设置,并且想写信给它;所以我的问题是,如何从 Azure 函数中访问该表?

function.json 如下:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "documentDB",
      "name": "outputDocument",
      "databaseName": "mycosmosdb",
      "collectionName": "tableOutput",
      "createIfNotExists": false,
      "connection": "mycostmosdb_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

编辑:

有两个人暗示,就这个问题而言,术语“表”和“集合”是同义词。如果我理解正确,那么情况似乎并非如此,因为即使我更改 function.json 中的集合名称以匹配我创建的表的名称,我也会收到相同的错误。

为了澄清 CosmosDB 表配置,我在数据浏览器中看到一个名为 TablesDB 的节点,它具有我创建的表的子节点。

最佳答案

您需要使用您尝试将新文档保存到的 Cosmos DB 集合的正确“collectionName”值来更新 Function.json。目前,您的 Cosmos DB 数据库(“mycosmosdb”)中没有名为“tableOutput”的集合。

此外,如果您希望 Azure 函数输出绑定(bind)在集合不存在时自动创建集合,则将 Function.json 属性“createIfNotExists”设置为“true”,它将在以下情况下创建集合:它不存在。

无论哪种方法都可以让您的代码正常运行,但您需要确保“collectionName”设置为您已设置的 Cosmos DB 集合的正确名称。

例如,尝试将 Function.json 更改为以下内容:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "type": "documentDB",
      "name": "outputDocument",
      "databaseName": "mycosmosdb",
      "collectionName": "MyTable",
      "createIfNotExists": true,
      "connection": "mycostmosdb_DOCUMENTDB",
      "direction": "out"
    }
  ],
  "disabled": false
}

关于c# - 从 Azure Function 内部添加到 Azure CosmosDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47699176/

相关文章:

azure - 通过子网连接 CosmosDB 并定义相应的连接字符串

c# - 从正在等待的事件返回反馈

c# - 如何在 Xamarin Android 中实现 JobScheduler

c# - 如何以编程方式访问操作系统中最常用的程序和程序的最新文件?

azure - 可以在 Azure DevOps 中定义数组变量 "variable group"

javascript - 是否可以在函数内使用变量动态调用 Azure 函数的 Cosmos DB 输入绑定(bind)的 SQL 查询?

azure - 从 Azure 流分析中的 EventHub 删除重复项

c# - 如何在我的 WinForm 应用程序中调试此 StackOverflowException?

azure - 使用 ODATA 从 Application Insights REST API 获取最旧的记录

c# - 发布到 Azure 的 Asp.Net 应用程序仅显示 "Your App Service app has been created"