Search code examples
microservicesasp.net-core-webapiazure-servicebus-topics

Microservice retrieving OrderId from an Azure service bus topic


I am trying to understand Azure service bus which I intend to use in a current project. I am completely new to this.

Here is the scenario. There are two microservices.

Microservice A - Writes order details to a database and also writes OrderId to an Azure service bus topic

Microservice B - Should be able to pick up the OrderId when ever this exists from the same topic and use it to process some other transactions. There can be multiple OrderId's generated in a day by users.

How do I setup Microservice B to perform this duty? How does this work in reality? How can this microservice constantly monitor the topic?


Solution

  • You can do write a job to check new message every "X" seconds or continuously but that's not a good approach. A better way is Pub-sub approach .

    In pub-sub when message arrives at the topic , any service subscribed to topic receive the message and doesn't need to poll for it. Your Microservice B should be subscribed to the topic and message will be pushed to it.

    For your microservice B to subscribed to message you have two options. Either you can azure function which will automatically process the message for you. If you don't want to write the azure function then you will have to use Azure Service Bus Library to get this done. The official example only accommodates console app (Pub-Sub Azure Service Bus) however for .Net Core Web API you can look here Subscribe Azure Service Bus