Following along in this Azure Service Bus Tutorial I've taken the code and mostly pasted it into my ASP.Net Core app controller 'MyController.cs', I'm not following the tutorial verbatim with a console app because I already have a website where I want to send and receive messages.
I can send messages to the queue correctly, but after I implemented the code to listen for and receive messages, I start continually receiving errors in the 'ErrorHandler()' method that says:
System.ObjectDisposedException: AmqpConnectionScope has already been closed and cannot perform the requested operation.
QUESTIONS -
- Is this the correct code below needed to listen for incoming messages in a ASP.Net Core app controller? I looked through the docs but can't seem to find any other examples that might handle this scenario?
- I do see a code sample where MS is using an Azure function to listen for incoming service bus messages. Is that the prescribed way to listen if I'm not using a console app? I assumed it would be easier to have all code (sending/receiving) in one application?
Here is all the code from my controller related to service bus calls. FYI - I've commented out 'StopProcessingAsync()' as it seems like it wasn't needed to perform my testing.
[HttpPost("test")] public async Task<IActionResult> OnTesting(TestDto testDto) { // some code here left out await SendMessageAsync(); await ReceiveMessagesAsync(); } static async Task SendMessageAsync() { var connectionString = "my secret conn string"; var queueName = "testqueue"; // create a Service Bus client await using (ServiceBusClient client = new ServiceBusClient(connectionString)) { // create a sender for the queue ServiceBusSender sender = client.CreateSender(queueName); // create a message that we can send ServiceBusMessage message = new ServiceBusMessage("Hello world!"); // send the message await sender.SendMessageAsync(message); Console.WriteLine($"Sent a single message to the queue: {queueName}"); } } static async Task ReceiveMessagesAsync() { var connectionString = "same as above conn string"; var queueName = "testqueue"; await using (ServiceBusClient client = new ServiceBusClient(connectionString)) { // create a processor that we can use to process the messages ServiceBusProcessor processor = client.CreateProcessor(queueName, new ServiceBusProcessorOptions()); // add handler to process messages processor.ProcessMessageAsync += MessageHandler; // add handler to process any errors processor.ProcessErrorAsync += ErrorHandler; // start processing await processor.StartProcessingAsync(); // Console.WriteLine("Wait for a minute and then press any key to end the processing"); // Console.ReadKey(); // stop processing // Console.WriteLine("\nStopping the receiver..."); // await processor.StopProcessingAsync(); // Console.WriteLine("Stopped receiving messages"); } } // handle received messages static async Task MessageHandler(ProcessMessageEventArgs args) { string body = args.Message.Body.ToString(); Console.WriteLine($"Received: {body}"); // complete the message. messages is deleted from the queue. await args.CompleteMessageAsync(args.Message); } // handle any errors when receiving messages static Task ErrorHandler(ProcessErrorEventArgs args) { Console.WriteLine(args.Exception.ToString()); // errors coming in after listening starts return Task.CompletedTask; }
https://stackoverflow.com/questions/67351828/error-when-listening-for-azure-service-bus-queue-messages-in-asp-net-core-app-co May 02, 2021 at 07:49AM
没有评论:
发表评论