0

We have multiple webapi(microservices). To communicate between these micro services we are using Azure service bus Topic\Subscription. How webapi controller can get the message subscription so that api can process the message and insert to database. I don't want to use any azure functions. I want subscribe message in my webapi(micro service directly).

CHEEKATLAPRADEEP
  • 12,191
  • 1
  • 19
  • 42
  • A controller needs to be invoked. So polling / long polling is not an option. You could leverage Azure Event Grid to invoke a controller (as a webhook), but at this point this is only available on Premium tier and is intended for systems with low message throughput. Alternative could be a WebJob if you're on Azure App Service. And even then something feels off about this setup. – Sean Feldman Sep 07 '18 at 02:46

1 Answers1

1

You may create a client for the topic to subscribe the message and set the options to complete after the necessary process is done. please find the below sample code, where you can read the subscription messages from a topic.

you can use WindowsAzure.ServiceBus -Nuget Package

Sample Code:

using Microsoft.ServiceBus.Messaging;
using Microsoft.ServiceBus;
var con = "your connection string for the topic";
var topic = "your topic name";
var subclient = SubscriptionClient.CreateFromConnectionString(con, topic, "yourSubscriptionName");

OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
client.OnMessage(message =>
{
Console.WriteLine("Message:" + message.GetBody<string>());
message.Complete();
}, options);

you may put it in separate method and use as needful. To poll you may create a webjob, or you can place the code which triggers the web api, so that it will poll the messages at the same time.

Luis Gouveia
  • 8,334
  • 9
  • 46
  • 68