Cosmos DB 与 Azure 函数和 SignalR 的集成

Integration of CosmosDB with Azure fuction and SignalR

我正在处理 Microsoft 在下面的 link 中提供的示例 https://anthonychu.ca/post/cosmosdb-real-time-azure-functions-signalr-service/ 我已按照所有步骤操作,但在 运行 本地带有 azure 函数的代码时出现以下错误:

The listener for function 'Functions.OnDocumentsChanged' was unable to start. [2/18/2019 9:50:54 PM] The listener for function 'Functions.OnDocumentsChanged' was unable to start. Microsoft.Azure.Documents.ChangeFeedProcessor: The lease collection, if partitioned, must have partition key equal to id

错误信息很清楚,触发器使用辅助(租约)集合来存储状态。在您的触发器定义中,您可以在 Configuration 中指定是否要指定特定的 Leases 集合名称/数据库名称或保留默认值 ("leases")。 如果 Leases 集合不存在,触发器也可以通过 CreateLeaseCollectionIfNotExists 属性为您创建它。

在您的情况下,您似乎已经拥有之前创建的租约集合。

问题是租约集合,如果它是分区的,它需要按 /id ,这就是错误消息所说的:

The lease collection, if partitioned, must have partition key equal to id

因此,要解决此问题,您可以:

  1. 删除您当前的租约集合并使用 CreateLeaseCollectionIfNotExists = true 让触发器为您创建它。
  2. 手动创建租约集合并将 /id 设置为分区键。