CodexBloom - Programming Q&A Platform

How to implement Azure Service Bus sessions with ASP.NET Core for ordered message processing?

👀 Views: 90 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-09
azure service-bus aspnet-core csharp

I've hit a wall trying to I'm learning this framework and I've been working on this all day and I'm getting frustrated with I'm trying to implement Azure Service Bus with session support in my ASP.NET Core application to ensure ordered message processing. I have set up a service bus queue with sessions enabled, but I'm working with issues when sending and receiving messages. My current implementation for sending messages looks like this: ```csharp var client = new ServiceBusClient("<connection_string>"); var sender = client.CreateSender("<queue_name>"); var message = new ServiceBusMessage("This is a test message") { SessionId = "session-id-1" }; await sender.SendMessageAsync(message); ``` When it comes to receiving messages, I have the following code: ```csharp var processor = client.CreateSessionReceiver("<queue_name>", new ServiceBusSessionReceiverOptions() { MaxConcurrentSessions = 1 }); await processor.AcceptSessionAsync("session-id-1"); var receivedMessage = await processor.ReceiveMessageAsync(); ``` However, I keep getting the behavior: `Session 'session-id-1' not found`. I've verified that the session ID I am using to send the message matches the one I am trying to receive with. I've also checked the Azure portal, and it shows that messages are present in the queue. I've tried using the `ServiceBusSessionReceiverOptions` to adjust `MaxConcurrentSessions`, but the scenario continues. Additionally, I enabled logging and noticed that the messages are being sent correctly, but for some reason, they are not being associated with the session. Any advice on what I might be doing wrong or any additional configurations that I might be missing would be greatly appreciated! I'm using Azure.Messaging.ServiceBus version 7.9.0 and .NET 5. Any ideas how to fix this? I'm working on a service that needs to handle this. Thanks for taking the time to read this! I'm coming from a different tech stack and learning Csharp. Has anyone else encountered this? For reference, this is a production REST API. I'd love to hear your thoughts on this. I appreciate any insights! Is there a simpler solution I'm overlooking?