Search code examples
azureazureservicebus

Azure Service Bus - REST API - Sending a message to a Session Enabled Topic/Subscription


I am sending messages to a Azure Service Bus topic using REST API - https://learn.microsoft.com/en-us/rest/api/servicebus/send-message-to-queue.

The messages are getting dead lettered with the reason "Session enabled entity doesn't allow a message whose session identifier is null". Since the subscription is session enabled, a "sessionId" is expected to be passed. I see that the SDK supports sending a sessionId but the REST API does not. But I need to use the REST API and can't connect using SDKs because the call is made from within a vended product.

Any thoughts on how to overcome this issue? How can a sessionId be passed via the API?


Solution

  • Thanks @Thomas for your input but the SessionId property you mentioned is part of the .NET SDK for Azure Service Bus, which is available when working within a .NET application that references the Microsoft.ServiceBus.Messaging namespace.

    This property allows you to set the session identifier for a message when sending it to a session-enabled queue or topic using the SDK.

    However, since you're constrained to using the REST API and cannot utilize the SDK, you'll need to find a way to mimic the functionality of setting the SessionId through the REST API.

    • Implementing a small proxy or using an Azure Function or Logic App might be your best bet to inject the SessionId when sending messages.

    consoleapp.cs with Rest API using SessionId:

    using System;
    using System.IO;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
     
    class Program
    {
        static async Task Main(string[] args)
        {
            // Replace with your Service Bus details
            string serviceNamespace = "sampath";
            string queueName = "sampath";
            string sasToken = "";
            string messageBody = "This is a message.";
     
            // Create the HTTP client
            using (HttpClient client = new HttpClient())
            {
                // Set the request URI
                string requestUri = $"https://{serviceNamespace}.servicebus.windows.net/{queueName}/messages?timeout=60";
     
                // Create the message content with corrected content type
                StringContent content = new StringContent(messageBody, Encoding.UTF8, "application/xml");
     
                // Add required headers
                client.DefaultRequestHeaders.Add("Authorization", sasToken);
     
                // Adding the BrokerProperties header with SessionId
                client.DefaultRequestHeaders.Add("BrokerProperties", "{\"Label\":\"M1\",\"State\":\"Active\",\"TimeToLive\":10,\"SessionId\":\"Hello\"}");
     
                client.DefaultRequestHeaders.Add("Priority", "High");
                client.DefaultRequestHeaders.Add("Customer", "12345,ABC");
                client.DefaultRequestHeaders.ExpectContinue = true;
     
                
                HttpResponseMessage response = await client.PostAsync(requestUri, content);
     
                // Check the response
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine($"Message sent successfully with status code: {response.StatusCode}");
                }
                else
                {
                    Console.WriteLine($"Failed to send message. Status code: {response.StatusCode}, Reason: {response.ReasonPhrase}");
                }
            }
        }
    }
    

    Message sent:

    enter image description here

    Output:

    enter image description here