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.
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.