c# - 带有 CosmosDBTrigger 的 Azure 函数似乎不是由 upsert 触发的

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

我是第一次使用 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;.

我运行该函数,然后将文档添加到集合中,但日志一直显示在过去的 n 分钟内没有新的跟踪并且我期望看到的应用程序事件没有被执行写的。

我在这个设置中错过了什么吗?

最佳答案

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

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

即使对于监听 Cosmos DB 更改源的单个函数来说,它也是必需的。

关于c# - 带有 CosmosDBTrigger 的 Azure 函数似乎不是由 upsert 触发的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52115599/

相关文章:

c# - 单击 Return 时 TextBox 是否应该引发验证事件?

asp.net - JsonResult 在浏览器中显示文件下载

javascript - 如何通过asp.net获取表单提交后的post数据

postgresql - Azure PostgreSQL 灵活服务器 Cron 作业权限被拒绝

Azure辅助角色模拟域帐户

c# - 对于需要精确到 .00001 的翻译,Decimal 或 Double 会更好吗?

c# - 如何使 .NET 反射与动态生成的对象一起工作?

c# - 比较 SynchronizationContext

asp.net - 如何在 Azure 中对多个 Web/辅助角色实例进行内部负载平衡?

asp.net - 为 Azure Active Directory 中的用户提供角色