azure 显示服务总线的数据/消息

k4ymrczo  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(120)

我想从某个日期从服务总线读取消息,但我的代码不阅读消息.在服务总线有任何选项,如设置触发器,我已经尝试了下面的代码从微软文档,但仍然无法接收消息.

消息处理程序

public static async Task<int> Main(string[] args)
    {
        await AdditionAsync();

        return 0;
    }

    static async Task MessageHandler(ProcessMessageEventArgs args)
    {
        var body = args.Message.Body.ToString();
        Console.WriteLine(body);
        if (body.Contains("My Label")) 
        {
            Console.WriteLine($"Received: {body} from subscription: <TOPIC-SUBSCRIPTION-NAME>");
        } 
    }

错误处理程序

static Task ErrorHandler(ProcessErrorEventArgs args)
    {
        Console.WriteLine(args.Exception.ToString());
        return Task.CompletedTask;
    }

    private static async Task AdditionAsync()
    {

        var clientOptions = new ServiceBusClientOptions
        {
            TransportType = ServiceBusTransportType.AmqpWebSockets
        };

        var client = new ServiceBusClient(
            "Connection string",
             clientOptions);
        
        var test = new ServiceBusProcessorOptions()
        {

        }
     
        var processor = client.CreateProcessor("TOpic", "subscription", new ServiceBusProcessorOptions());
  
        try
        {
           
            processor.ProcessMessageAsync += MessageHandler;

            
            processor.ProcessErrorAsync += ErrorHandler;

            
            await processor.StartProcessingAsync();

            Console.WriteLine("Wait for a minute and then press any key to end the processing");
            Console.ReadKey();

            
            Console.WriteLine("\nStopping the receiver...");
            await processor.StopProcessingAsync();
            Console.WriteLine("Stopped receiving messages");
        }
        finally
        {
            
            await processor.DisposeAsync();
            await client.DisposeAsync();
        }

        Console.WriteLine("DONE");

        Console.ReadLine();

    }
}

我们能不能换个密码,在特定的日期收到信息。

62o28rlo

62o28rlo1#

在Azure门户中创建了一个服务总线现在转到资源并转到需要发送消息的服务总线资源管理器。在此之前,在服务总线中创建一个订阅,然后按如下所示发送消息。

现在要在特定日期检索消息,请使用以下代码

using Azure.Messaging.ServiceBus;
using System;
string connectionString = "<connection_string>";
string entityName = "<queue_or_topic_name>";
ServiceBusClient client = new ServiceBusClient(connectionString);
ServiceBusReceiver receiver = client.CreateReceiver(entityName, new ServiceBusReceiverOptions
{
    ReceiveMode = ServiceBusReceiveMode.ReceiveAndDelete, // Delete messages immediately after receiving them
    MaxWaitTime = TimeSpan.FromSeconds(5) // Maximum time to wait for a message to become available
});
Console.Write("Enter a date and time (yyyy-MM-dd HH:mm:ss): ");
string input = Console.ReadLine();
if (!DateTimeOffset.TryParse(input, out DateTimeOffset dateTime))
{
    Console.WriteLine("Invalid date and time format.");
    return;
}
while ((message = await receiver.ReceiveMessageAsync(dateTime)) != null)
{
    Console.WriteLine($"Received message with body: {message.Body}");
}
await receiver.CloseAsync();
await client.DisposeAsync();

通过替换连接字符串、订阅名称和主题名称来运行上面的代码。我在我的环境中做了同样的事情,并在所需的日期获得了消息。

相关问题