Azure Fuction DARP Publish 在 Azure 服务总线消息中设置计划选项

问题描述 投票:0回答:1

我想创建一个 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 中设置消息调度。

c# .net azure-functions azureservicebus dapr
1个回答
0
投票

一种方法是使用 Azure 服务总线的 .NET SDK 来安排消息。

  • 预定的消息将被发送到延迟处理的队列/主题。
  • 下面的代码将事件发布到 Dapr 发布/订阅主题,将消息发送到 Azure 服务总线主题,并使用
    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);
        }
    }

enter image description here

enter image description here

蔚蓝: enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.