0

I am using Azure Service bus to pass messages between two .Net core Web API services. I am suceesfully able to set up topic/subscription in Azure and also able to send the message to topic, using the Web API post method. I m stuck when I am trying to read the message using the subscription. I have written an API method which is trying to read the message and it should also display the message after reading it from topic. When I use .Net core Console APP I am able to read the message , but when using API, message is not displayed as API output.

Any leads as to what I am missing ? Thanks in advance.

Code is below :

public async Task Get Subscription() {

            subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName);

            RegisterOnMessageHandlerAndReceiveMessages();
            await subscriptionClient.CloseAsync();
     }

public void RegisterOnMessageHandlerAndReceiveMessages() {

            var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler);

            subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);

    }

private async Task ProcessMessagesAsync(Message message, CancellationToken token) {
var messageBody = Encoding.UTF8.GetString(message.Body);

            var serviceBusMessage = JsonConvert.DeserializeObject<ServiceBusMessage>(messageBody);

    }
user2294434
  • 123
  • 4
  • 16

1 Answers1

0

You're mixing two paradigms. The handler approach runs in the background and receives a single message into the callback when there are available messages. You can scale that out by upping the concurrency of the callback. That won't help you as your Web API needs to return back the response and the controller/action needs the message upon request and not when the callback (Service Bus handler code) is invoked.

You need to use MessageReceiver. With a message receiver, you do not set up a callback. The invocation will result in either received one (or more, if needed) message or null if there are no messages to receive.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80