skip to Main Content

I am using the Azure Service Bus API to send a message into a topic. This is the API – https://learn.microsoft.com/en-us/rest/api/servicebus/send-message-to-queue

I am getting a 201 as expected, so the call is successful. But the message gets delivered into the Dead Letter Queue.Since I am calling via the API, I do not get a DeadLetterErrorDescription or anything like that back which I would normally get if I were using the SDK.

I researched and found that some folks have overcome this issue by passing a session id, which again would be possible only if I am using the SDK.

Any thoughts on how to troubleshoot this issue?

2

Answers


  1. Chosen as BEST ANSWER

    I was able to find out the reason why the messages got dead lettered. The dead letter error description is "Session enabled entity doesn't allow a message whose session identifier is null". So because the topic is setup as session enabled, a session id is expected to be passed.

    While using the REST API to send messages, a session id can be passed by setting a header property named "BrokerProperties". The documentation for that is here - https://learn.microsoft.com/en-us/rest/api/servicebus/message-headers-and-properties

    By setting a header property like the one below, I was able to send a message and it reached the topic/subscription successfully without getting dead lettered.

    "BrokerProperties" as key and "{“SessionId”: “{1234}”" as value


  2. Using below code, we can send the message successfully and i have followed Microsoft_Document:

    Used rest api https://{ri_name_space}.servicebus.windows.net/{ri_topic_name}/messages:

        using System.Text;
        using System.Web;
        using System.Security.Cryptography;
        using Newtonsoft.Json;
        
        namespace RithBus
        {
            class Program
            {
                static async Task Main(string[] args)
                {
                    var ri_name_space = "ServiceBusName";
                    var ri_topic_name = "TopicName";
                    var ri_skn = "RootManageSharedAccessKey";
                    var ri_primary_Key = "6YXOZPAvs3JrithhY=";
                    var ri_url = $"https://{ri_name_space}.servicebus.windows.net/{ri_topic_name}/messages";
                    TimeSpan expirySinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1) + TimeSpan.FromHours(1);
                    var ri_exp = Convert.ToString((int)expirySinceEpoch.TotalSeconds);
                    string rst = HttpUtility.UrlEncode(ri_url) + "n" + ri_exp;
                    using var tst = new HMACSHA256(Encoding.UTF8.GetBytes(ri_primary_Key));
                    var ri_sign = Convert.ToBase64String(tst.ComputeHash(Encoding.UTF8.GetBytes(rst)));
                    var ri_SAS = String.Format("SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(ri_url), HttpUtility.UrlEncode(ri_sign), ri_exp, ri_skn);
                    using var riclnt = new HttpClient();
                    riclnt.DefaultRequestHeaders.Add("Authorization", ri_SAS);
                    var ri_data = new { message = "Hello Rithwik Bojja!!!" };
                    var ris = new StringContent(JsonConvert.SerializeObject(ri_data), Encoding.UTF8, "application/json");
                    var ri_out = await riclnt.PostAsync(ri_url, ris);
                    Console.WriteLine("Hlo, Rithwik Msg sent successfully");
                }
            }
        }
    

    Output:

    enter image description here

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search