修改数据库名后Azure Function Cosmos DB触发器启动报错求助
Azure Cosmos DB触发器启动报错:This builder instance has already been used to build a processor
问题场景
在Windows 11系统上使用Azure Cosmos DB模拟器,通过Azure Function的Cosmos DB触发器监听容器变更。修改触发器的数据库名称参数后,启动Function时出现启动失败错误。
错误日志
[2023-12-26T17:16:04.200Z] Starting the listener for prefix='', monitoredContainer='business_docs_02', monitoredDatabase='cosmicworks_02', leaseContainer='leases_docs', leaseDatabase='cosmicworks_02', functionId='Host.Functions.MyCoFunc2' failed. Exception: System.InvalidOperationException: This builder instance has already been used to build a processor. Create a new instance to build another. [2023-12-26T17:16:04.205Z] at Microsoft.Azure.Cosmos.ChangeFeedProcessorBuilder.Build() [2023-12-26T17:16:04.211Z] at Microsoft.Azure.WebJobs.Extensions.CosmosDB.CosmosDBTriggerListener`1.StartProcessorAsync() in D:\a\_work\1\s\src\WebJobs.Extensions.CosmosDB\Trigger\CosmosDBTriggerListener.cs:line 140 [2023-12-26T17:16:04.214Z] at Microsoft.Azure.WebJobs.Extensions.CosmosDB.CosmosDBTriggerListener`1.StartAsync(CancellationToken cancellationToken) in D:\a\_work\1\s\src\WebJobs.Extensions.CosmosDB\Trigger\CosmosDBTriggerListener.cs:line 96. [2023-12-26T17:16:04.216Z] The listener for function 'Functions.MyCoFunc2' was unable to start. [2023-12-26T17:16:04.217Z] The listener for function 'Functions.MyCoFunc2' was unable to start. Microsoft.Azure.Cosmos.Client: This builder instance has already been used to build a processor. Create a new instance to build another.
触发器配置代码
public void Run([CosmosDBTrigger( databaseName: "cosmicworks_02", containerName: "business_docs_02", Connection = "DBConnectionString", LeaseContainerName = "leases_docs", CreateLeaseContainerIfNotExists = true )] IReadOnlyList<MyDocument> input)
解决方案
- 避免共享CosmosClient实例:确保每个CosmosDBTrigger使用独立的CosmosClient实例。如果在依赖注入中注册了单例CosmosClient,改为使用客户端工厂模式,每次创建新实例供触发器使用,防止ChangeFeedProcessorBuilder被重复调用。
- 清理租约容器数据:修改数据库名称后,旧租约容器(
leases_docs)中可能残留了对应旧数据库的租约记录,导致启动冲突。手动删除租约容器内的所有文档,或者直接删除租约容器,让触发器自动重建。 - 更新依赖包版本:检查
Microsoft.Azure.WebJobs.Extensions.CosmosDB包的版本,旧版本可能存在Builder复用的bug。升级到最新稳定版后重启Function。 - 重启宿主和模拟器:Function宿主进程可能缓存了旧的Builder实例,重启Function宿主和Cosmos DB模拟器,清除缓存后重新启动。
内容的提问来源于stack exchange,提问作者toto'
相关产品推荐
相关产品推荐

