0

Whenever I send a message to the topic I am unable to receive it from the subscription. From my understand this should be possible. Not sure what in the code is currently making it unable to forward the message from the topic to the subscription.

static void main(string[] args)
{
 MainAsync().GetAwaiter().GetResult();
}

 static async Task MainAsync()
{
var factory = MessagingFactory.CreateFromConnectionString(connectionstring);
var receiver = await factory.CreateMessageReceiverAsync(topic name + "/subcriptions/" +subcriptionName);
var sender = await factory.CreateMessageSenderAsync(topicName);
// receive message from subscription
receiver.OnMessageAsync( async receivedMessage =>
{
Console.WriteLine("receive message - {0}, receivedMessage.MessageId);
await receivedMessage.CompleteAsync();
}, new OnMessageOptions() {AutoComplete = false});

// send message to topic
await sender.SendAsync(new BrokeredMessage("testing") {MessageId ="dhfs8264"});

await Task.WhenAny(
Task.Run(() => Console.Readkey()),
Task.Delay(TimeSpan.FromSeconds(10)));
}}}
  • You are creating the message receiver incorrectly...see message processor in this example https://learn.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-how-to-use-topics-subscriptions – Anand Sowmithiran Dec 31 '22 at 03:31

1 Answers1

0

I tried in my environment and got below results:

Make sure you were using latest version of NuGet package for Azure-Messaging-ServiceBus- 7.11.1 in your environment because Microsoft.Azure.ServiceBus is deprecated.

Initially, I created service bus with topic and subscription through portal using this Document and sending 5 messages to the topic.

Sending messages to the topic with below code:

using System.Diagnostics;
using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;


   ServiceBusClient client;
   ServiceBusSender sender;
   const int numOfMessages = 5;
   client = new ServiceBusClient("<connect strng >");
   sender = client.CreateSender("topicname");
   using ServiceBusMessageBatch messageBatch = await sender.CreateMessageBatchAsync();

   for (int i = 1; i <= numOfMessages; i++)
   {
       if (!messageBatch.TryAddMessage(new ServiceBusMessage($"Message {i}")))
       {
           throw new Exception($"The message {i} is too large to fit in the batch.");
       }
   }
   try
   {
       await sender.SendMessagesAsync(messageBatch);
       Console.WriteLine($"A batch of {numOfMessages} messages has been published to the topic.");
   }
   finally
   {
       await sender.DisposeAsync();
       await client.DisposeAsync();
   }

   Console.WriteLine("Press any key to end the application");
   Console.ReadKey();

Console: enter image description here

Portal: In portal you can check in the overview tab of incoming messages is 5. enter image description here

Receiving messages from a subscription with below code:

using System;
using System.Threading.Tasks;
using Azure.Messaging.ServiceBus;

ServiceBusClient client;
ServiceBusProcessor processor;
async Task MessageHandler(ProcessMessageEventArgs args)
{
    string body = args.Message.Body.ToString();
    Console.WriteLine($"Received: {body} from subscription.");
    await args.CompleteMessageAsync(args.Message);
}
Task ErrorHandler(ProcessErrorEventArgs args)
{
    Console.WriteLine(args.Exception.ToString());
    return Task.CompletedTask;
}

client = new ServiceBusClient("connect strng");
processor = client.CreateProcessor("topicname", "subscriptionname", new ServiceBusProcessorOptions());

try
{
    processor.ProcessMessageAsync += MessageHandler;
    processor.ProcessErrorAsync += ErrorHandler;
    await processor.StartProcessingAsync();
    Console.WriteLine("Wait for a minute and then press any key to end the processing");
    Console.ReadKey();
    Console.WriteLine("\nStopping the receiver...");
    Console.WriteLine("Stopped receiving messages");
}
finally
{ 
    await processor.DisposeAsync();
    await client.DisposeAsync();
}

Console:

The above code executed successfully and received messages from subscription. enter image description here

Portal: enter image description here

Portal: enter image description here

At last, in your Service Bus Subscription level page, you see the Active message count as zero. Because it tells receiver has read and finished messages from this subscription.

Venkatesan
  • 3,748
  • 1
  • 3
  • 15