You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 04:15:05