0
votes

I am creating a sample Azure Service Bus application. I created a namespace, topic and Subscription. I have written test messages to the topic and if I go to the subscription in the portal, I see that I have a new message every time I write a new one using the writer application.

But when I go to pull the message, nothing is retrieved. In troubleshooting, I changed the subscription name to an incorrect value and received an error. I changed it back and I get no output and none of the messages are removed when I look in Azure portal. I'm stuck... this seems easy, but it isn't working.

string connectionString = "Endpoint=sb://redacted for obvious reasons";

        SubscriptionClient Client = SubscriptionClient.CreateFromConnectionString(connectionString, "NewOrders", "AllOrders");

        // Configure the callback options.
        OnMessageOptions options = new OnMessageOptions();
        options.AutoComplete = false;
        options.AutoRenewTimeout = TimeSpan.FromMinutes(1);

        Client.OnMessage((message) =>
        {
            try
            {
                Console.WriteLine("Body: " + message.GetBody<string>());

                message.Complete();
                Console.ReadLine();
            }
            catch (Exception)
            {
                // Indicates a problem, unlock message in subscription.
                message.Abandon();
            }
        }, options);
1
How your subscription AllOrders is configured? Can you share a repro of your code on GitHub?Sean Feldman
Are you running a console app ? do you block the thread not to terminate the app ?Thomas

1 Answers

1
votes

It seems that it is not the code issue. I create a demo to retrieve message from topic, it works correctly. Before I try to retrieve message from topic, I send the message to the topic or make sure that there are messages for subscription. We could check that from the portal

enter image description here

Send message code demo.

 private static void SendMessages()
 {
    topicClient = TopicClient.Create(TopicName);

    List<BrokeredMessage> messageList = new List<BrokeredMessage>
    {
       CreateSampleMessage("1", "First message information"),
       CreateSampleMessage("2", "Second message information"),
       CreateSampleMessage("3", "Third message information")
    };

    Console.WriteLine("Sending messages to topic...");
    foreach (BrokeredMessage message in messageList)
    {
       while (true)
       {
          try
          {
             topicClient.Send(message);
          }
          catch (MessagingException e)
          {
              if (!e.IsTransient)
              {
                  Console.WriteLine(e.Message);
                  throw;
              }
           }
           Console.WriteLine($"Message sent: Id = {message.MessageId}, Body = {message.GetBody<string>()}");
           break;
        }
    }

      topicClient.Close();
}

I also try your mentioned code, it also works correctly for me.

enter image description here

enter image description here

We also could get the demo project from the cloud Project from the template. We also could get more info about How to use Service Bus topics and subscriptions from the document.