英文:
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<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>");
}
}
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(
"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();
}
}
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.
Now to retrieve the messages on a particular date use the code below
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();
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论