【问题标题】:Azure function with CosmosDBTrigger doesn't seem to be triggered by upserts带有 CosmosDBTrigger 的 Azure 函数似乎没有被 upserts 触发
【发布时间】:2018-08-31 12:35:01
【问题描述】:

我是第一次使用 Azure Functions。我正在尝试编写一个简单的函数来响应更改或添加到 CosmosDb 集合的文档。我写的函数是这样的:

[FunctionName("ChangeLog")]
public static void Run([CosmosDBTrigger(
    databaseName: "Recaptcha",
    collectionName: "Rules",
    ConnectionStringSetting = "CosmosDBConnection",
    LeaseCollectionName = null)]IReadOnlyList<RuleConfigCollection> documents)
{
    if (documents != null && documents.Count > 0)
    {
        ApplicationEventLogger.Write(
            Diagnostics.ApplicationEvents.RecaptchaRulesChanged,
            new Dictionary<string, object>()
            {
                { "SomeEnrichment", documents[0].Rules.ToList().Count.ToString() }
            });
    }
}

据我了解,当多个函数指向同一个 CosmosDb 时,租赁集合是必要的,但在我的情况下,这无关紧要。这就是为什么我将租约集合设置为null

我已将其从 Visual Studio 发布到 Azure,并且可以看到该函数是使用以下 function.json 创建的:

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.12",
  "configurationSource": "attributes",
  "bindings": [
    {
      "type": "cosmosDBTrigger",
      "connectionStringSetting": "CosmosDBConnection",
      "collectionName": "Rules",
      "databaseName": "Recaptcha",
      "leaseDatabaseName": "Recaptcha",
      "createLeaseCollectionIfNotExists": false,
      "name": "documents"
    }
  ],
  "disabled": false,
  "scriptFile": "../bin/My.Namespace.Functions.App.dll",
  "entryPoint": "My.Namespace.Functions.App.ChangeLogFunction.Run"
}

我还添加了一个名为CosmosDBConnection 的应用程序设置,其值为AccountEndpoint=https://my-cosmosdb.documents.azure.com:443;AccountKey=myAccountKey;

我运行该函数,然后将一个文档添加到集合中,但日志一直显示No new trace in the past n min(s),并且我希望看到的应用程序事件没有被写入。

我在这个设置中遗漏了什么吗?

【问题讨论】:

  • 好的,我现在实际上从我自己的一个依赖项中抛出了一个异常,所以我的函数正在被调用。不确定这是否是因为我根据@mikhail 在下面的评论添加了租约集合。但看起来这现在是我这边的应用程序问题。
  • 我更改的另一件事是将RuleConfigCollection(我的强类型文档模型)替换为通用Microsoft.Azure.Documents.Document。稍后我将不得不研究强类型。

标签: c# asp.net azure azure-functions azure-cosmosdb


【解决方案1】:

我不确定这是您问题的根本原因,但您对leaseCollection 的理解是错误的。

leaseCollection 用于协调您的 Function App 的多个实例(工作人员)以在工作人员之间分配分区。

即使是单个函数也需要监听 Co​​smos DB 更改源。

【讨论】:

  • 感谢您的澄清。但是,即使我指定 LeaseCollectionName 并将 CreateLeaseCollectionIfNotExists 设置为 true 我也可以看到正在创建此集合,但在将文档添加到触发集合后该函数永远不会运行。
猜你喜欢
  • 1970-01-01
  • 2018-11-17
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-26
相关资源
最近更新 更多