我想创建一个 Azure 函数(使用 C# 和 DARP API),当消息进入 Azure 服务总线时触发该函数,然后将测试消息发布到 Azure 服务总线中的主题。但我不希望立即发布该消息。我已经调查过,但没有看到我在 DaprPubSubEvent 中设置的任何计划选项。下面是功能代码。
public static class TestFunc
{
[FunctionName("TestFunc")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "TestFunc")] HttpRequest req,
[DaprPublish(PubSubName = "testSub", Topic = "testTopic")] IAsyncCollector<DaprPubSubEvent> pubEvent,
ILogger log)
{
log.LogInformation(" HTTP trigger function processed a request.");
await pubEvent.AddAsync(new DaprPubSubEvent("Test Message"));
return new OkObjectResult("TestResponse" );
}
请建议是否有办法在 DAPR 中设置消息调度。
一种方法是使用 Azure 服务总线的 .NET SDK 来安排消息。
ScheduledEnqueueTime
属性在特定时间安排消息排队。
[FunctionName("TestFunc")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "TestFunc")] HttpRequest req,
[DaprPublish(PubSubName = "testSub", Topic = "testTopic")] IAsyncCollector<DaprPubSubEvent> pubEvent,
ILogger log)
{
log.LogInformation("HTTP trigger function processed a request.");
try
{
string connectionString = "your_service_bus_connection_string";
string topicName = "your_service_bus_topic";
// Create a ServiceBusClient
await using var client = new ServiceBusClient(connectionString);
// Create a sender for the topic
ServiceBusSender sender = client.CreateSender(topicName);
// Create a message that we can send
ServiceBusMessage message = new ServiceBusMessage("Test Message");
// Set the ScheduledEnqueueTime to indicate how long it should be delayed
message.ScheduledEnqueueTime = DateTimeOffset.Now.AddMinutes(2); // Example: 2 minutes delay
// Send the message to the topic
await sender.SendMessageAsync(message);
// Publish the Dapr event
await pubEvent.AddAsync(new DaprPubSubEvent("Test Message"));
return new OkObjectResult("TestResponse");
}
catch (Exception ex)
{
log.LogError($"Error: {ex.Message}");
return new StatusCodeResult(500);
}
}
蔚蓝: