Azure函数中Cosmos DB触发器未触发问题排查及解决
Cosmos DB触发器未触发的排查与解决
我有两个Azure函数,代码如下:
Function.cs
[Function("TestServiceBus")] public async Task ServiceBusReceivedMessageFunction( [ServiceBusTrigger("key-vault-test-queue", Connection = "ServiceBusConnection")] ServiceBusReceivedMessage message) { _logger.LogInformation($"Message ID: {message.MessageId}"); _logger.LogInformation($"Message Body: {message.Body}"); _logger.LogInformation($"Message Content-Type: {message.ContentType}"); SetSecretsValues(); await CosmoDBRead(); //await CreateContainerCosmoDB(); await UpdateCosmosDBItem(message.Body.ToString()); } [Function("CosmosFunctionTrigger")] public void Run([CosmosDBTrigger( databaseName: "ToDoList", containerName: "Items_v2", Connection = "ConnectionStrings", LeaseContainerName = "leases", CreateLeaseContainerIfNotExists = true)]IReadOnlyList<ToDoItem> input) { if (input != null && input.Count > 0) { _logger.LogInformation("Documents modified " + input.Count); _logger.LogInformation("First document Id " + input[0].id); } }
UpdateCosmosDBItem方法负责通过指定ID和分区键更新Cosmos DB项,代码如下:
private async Task UpdateCosmosDBItem(string message) { CosmosClient cosmoClient = new CosmosClient( accountEndpoint: this._endpointUri, new DefaultAzureCredential() ); Database database = cosmoClient.GetDatabase("ToDoList"); Container container = database.GetContainer("Items_v2"); ToDoItem readItem = await container.ReadItemAsync<ToDoItem>( id: "1234", partitionKey: new PartitionKey("1234") ); readItem.Description = message; await container.UpsertItemAsync<ToDoItem>(readItem); _logger.LogInformation("Item has been updated"); }
我原本认为当Cosmos DB中的数据发生变更时,CosmosFunctionTrigger函数会被触发,但实际并未触发,请问我哪里存在认知误区?
解决方案
问题原因是Azure门户中该函数的配置里缺少对应的连接字符串,之前拼写错误未被察觉。
内容的提问来源于stack exchange,提问作者DonDavid12
相关产品推荐
相关产品推荐

