我需要一些帮助来澄清一些事情。我有两个功能,如下所示
[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 和分区键更新项目,如下所示
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");
}
我的理解是,它应该触发 CosmoDBTrigger 函数,因为它检测到了变化。它不会触发该功能。我在这里不明白什么?
Cosmos DB 未触发
我使用 Visual Studio 使用运行时堆栈 dotnet 创建了 servicebus 触发函数和 cosmosdb 触发函数。 我对你的代码做了一些更改。检查下面:
服务总线功能:
using System;
using Azure.Messaging.ServiceBus;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace FunctionApp13334
{
public class Function1
{
[FunctionName("Function1")]
public static async Task ServiceBusReceivedMessageFunction(
[ServiceBusTrigger("firstqueue", Connection = "ServiceBusConnection")] ServiceBusReceivedMessage message,
ILogger logger)
{
logger.LogInformation($"Message ID: {message.MessageId}");
logger.LogInformation($"Message Body: {message.Body}");
logger.LogInformation($"Message Content-Type: {message.ContentType}"
}
}
}
Cosmosdb功能:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace FunctionApp13334
{
public static class Function
{
[FunctionName("Function")]
public static async Task RunAsync([CosmosDBTrigger(
databaseName: "ToDoList1",
containerName: "Items_v2",
Connection = "cosmoscon",
LeaseContainerName = "leases",
CreateLeaseContainerIfNotExists = true)]IReadOnlyList<ToDoItem> input,
ILogger log)
{
if (input != null && input.Count > 0)
{
log.LogInformation("Documents modified " + input.Count);
log.LogInformation("First document Id " + input[0].id);
foreach (var item in input)
{
await UpdateFunction.UpdateCosmosDBItemAsync(item.Description, log);
}
}
}
}
// Customize the model with your own desired properties
public class ToDoItem
{
public string id { get; set; }
public string Description { get; set; }
}
}
更新功能:
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.Logging;
namespace FunctionApp13334
{
public static class UpdateFunction
{
private static readonly string _endpointUri = "you-cosmos URI";
private static readonly string _databaseName = "ToDoList";
private static readonly string _containerName = "Items_v2";
private static readonly string _primaryKey = "your-cosmosdb-primarykey"; // Add your Cosmos DB primary key
static string message = "hello";
public static async Task UpdateCosmosDBItemAsync(string message, ILogger log)
{
var cosmosClient = new CosmosClient(
_endpointUri,
_primaryKey); // Use the Cosmos DB primary key for authentication
var database = cosmosClient.GetDatabase(_databaseName);
var container = database.GetContainer(_containerName);
// Implement your update logic here
// For demonstration, let's just log the message
log.LogInformation($"Updating Cosmos DB item with message: {message}");
}
}
}
local.settings.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"cosmoscon": "your-cosmosdb-conn-string",
"ServiceBusConnection": "your-servicebus conn-string",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
主机.json:
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
},
"enableLiveMetricsFilters": true
}
}
}
我能够从 servicebus 队列接收消息,并且在进行任何更改并在 Items 中更新后触发 cosmosdb 函数。检查下面:
输出:
Azure Functions Core Tools
Core Tools Version: 4.0.5530 Commit hash: N/A +c8883e7f3c06e2b424fbac033806c19d8d91418c (64-bit)
Function Runtime Version: 4.28.5.21962
[2024-02-25T07:42:25.556Z] Found C:\Users\v-ppesala\source\repos\FunctionApp13334\FunctionApp13334.csproj. Using for user secrets file configuration.
Functions:
Function: cosmosDBTrigger
Function1: serviceBusTrigger
For detailed output, run func with --verbose flag.
[2024-02-25T07:42:35.920Z] Host lock lease acquired by instance ID '000000000000000000000000D8932B9B'.
[2024-02-25T07:43:22.508Z] Executing 'Function' (Reason='New changes on container Items_v2 at 2024-02-25T07:43:22.3865888Z', Id=2eede4af-b168-4493-8766-68b89fe56414)
[2024-02-25T07:43:22.567Z] Documents modified 1
[2024-02-25T07:43:22.572Z] First document Id 89842948099878
[2024-02-25T07:43:22.620Z] Updating Cosmos DB item with message:
[2024-02-25T07:43:22.707Z] Executed 'Function' (Succeeded, Id=2eede4af-b168-4493-8766-68b89fe56414, Duration=279ms)
[2024-02-25T07:43:29.493Z] Executing 'Function' (Reason='New changes on container Items_v2 at 2024-02-25T07:43:29.4932896Z', Id=5a4e62a5-8a2d-4d0d-9beb-5e75c69f79e6)
[2024-02-25T07:43:29.498Z] Documents modified 1
[2024-02-25T07:43:29.502Z] First document Id 898429499878
[2024-02-25T07:43:29.543Z] Updating Cosmos DB item with message:
[2024-02-25T07:43:29.548Z] Executed 'Function' (Succeeded, Id=5a4e62a5-8a2d-4d0d-9beb-5e75c69f79e6, Duration=55ms)
[2024-02-25T07:43:57.618Z] Executing 'Function1' (Reason='(null)', Id=b921fa13-b96a-4579-a230-65fda203eeb8)
[2024-02-25T07:43:57.657Z] Trigger Details: MessageId: 1274a97776b246a59c8e47caf82ada9e, SequenceNumber: 4, DeliveryCount: 1, EnqueuedTimeUtc: 2024-02-25T07:43:57.6670000+00:00, LockedUntilUtc: 2024-02-25T07:44:57.6820000+00:00, SessionId: (null)
[2024-02-25T07:43:57.664Z] Message ID: 1274a97776b246a59c8e47caf82ada9e
[2024-02-25T07:43:57.676Z] Message Body: hello pavan
[2024-02-25T07:43:57.680Z] Message Content-Type:
[2024-02-25T07:43:57.684Z] Executed 'Function1' (Succeeded, Id=b921fa13-b96a-4579-a230-65fda203eeb8, Duration=162ms)