显示Service Bus的数据/消息。

huangapple go评论65阅读模式
英文:

Display the data/messages for the Service Bus

问题

I can provide a translation of the text you provided. Here is the translated part:

我想要从某个特定日期开始读取服务总线上的消息,但我的代码未能读取消息。在服务总线中是否有设置触发器的选项?我已尝试了来自Microsoft文档的以下代码,但仍然无法接收消息。

消息处理程序

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("等待一分钟,然后按任意键以结束处理");

        Console.ReadKey();

        
        Console.WriteLine("\n停止接收器...");
        await processor.StopProcessingAsync();
        Console.WriteLine("已停止接收消息");
    }
    finally
    {
        
        await processor.DisposeAsync();
        await client.DisposeAsync();
    }

    Console.WriteLine("完成");

    Console.ReadLine();

}

我们是否可以更改代码以在特定日期接收消息?

英文:

I want to read messages from service bus from a certain date, But my code is not reading messages.
In service Bus is there any option like setting a trigger,
I have tried the below code from microsoft doc but still not able to recive messages.

Message handler

 public static async Task&lt;int&gt; 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(&quot;My Label&quot;)) 
        {
            Console.WriteLine($&quot;Received: {body} from subscription: &lt;TOPIC-SUBSCRIPTION-NAME&gt;&quot;);
        } 
    }

Error handler

 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(
            &quot;Connection string&quot;,
             clientOptions);
        
        var test = new ServiceBusProcessorOptions()
        {

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

            
            processor.ProcessErrorAsync += ErrorHandler;

            
            await processor.StartProcessingAsync();

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

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

        Console.WriteLine(&quot;DONE&quot;);

        Console.ReadLine();

    }
}

Can we change the code and recive the messages on perticular date.

答案1

得分: 0

以下是翻译好的部分:

  • 在Azure门户中创建了一个服务总线。
  • 现在进入资源并转到"Service Bus Explorer",需要发送消息的地方。
  • 在此之前,在服务总线中创建一个订阅,然后按照下面的步骤发送消息。
  • 要检索特定日期的消息,请使用以下代码。
  • 运行上面的代码,替换连接字符串、订阅名称和主题名称。我在我的环境中执行了相同的操作,并在所需日期收到了消息。
英文:

Created a service bus in azure portal
Now go to resource and go to Service Bus Explorer where messages need to be sent.
Before that create a subscription in service bus and now send messages as below.
显示Service Bus的数据/消息。
Now to retrieve the messages on a particular date use the code below

using Azure.Messaging.ServiceBus;
using System;
string connectionString = &quot;&lt;connection_string&gt;&quot;;
string entityName = &quot;&lt;queue_or_topic_name&gt;&quot;;
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(&quot;Enter a date and time (yyyy-MM-dd HH:mm:ss): &quot;);
string input = Console.ReadLine();
if (!DateTimeOffset.TryParse(input, out DateTimeOffset dateTime))
{
    Console.WriteLine(&quot;Invalid date and time format.&quot;);
    return;
}
while ((message = await receiver.ReceiveMessageAsync(dateTime)) != null)
{
    Console.WriteLine($&quot;Received message with body: {message.Body}&quot;);
}
await receiver.CloseAsync();
await client.DisposeAsync();

Run the above code by replacing the connection string, subscription name and topic name. I did the same in my environment and got the message on a required date.
显示Service Bus的数据/消息。

huangapple
  • 本文由 发表于 2023年3月23日 10:56:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定