Search code examples
azureazure-functionsazure-servicebus-topics

Integrating Azure Service Bus Topic and Azure Function


I created an Azure Function and selected Azure Service Bus Topic as the trigger in Visual Studio 2019. I also created a Service Bus Topic in my Azure account and have the Primary Connection String and Primary Key.

My questions are:

  1. How can I integrate the Azure Service Bus with the Azure Function that I created? What attributes should be changed in the code generated?
  2. How can I send a message to Azure Service Bus Topic locally on my machine? Is there any application like SQL Management Studio that can connect to my Azure Service Bus resource?
[FunctionName("Function1")]
public static void Run([ServiceBusTrigger("mytopic", "mysubscription", Connection = "ConnectionString")]string mySbMsg, ILogger log)
{
   log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
}

Solution

  • You have to change the tree attributes, and add them to the local.settings.json: You will have your Trigger like this:

    [ServiceBusTrigger(
                    topicName: "%MyServiceBus.Topic%",
                    subscriptionName: "%MyServiceBus.Subscription%",
                    Connection = "MyServiceBus.Connection")]
    

    And your local.settings.json like this:

     {  
      "Values": {
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "PriorityBoardingFare.Topic": "TestEvent",
        "PriorityBoardingFare.Subscription": "Appl1cation1",
        "PriorityBoardingFare.Connection": "Endpoint=sb://xxxxxxxxx.windows.net/;SharedAccessKeyName=xxxxxxd;SharedAccessKey=adasdasdasdasdasdasd"    
      },
      "Host": {
        "LocalHttpPort": 21094
      }
    }
    

    https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=windows%2Ccsharp%2Cbash#local-settings-file

    To send or manage the service bus you have now available two options:

    1. This is what we use at my work, its a community tool to manage the SB: https://github.com/paolosalvatori/ServiceBusExplorer
    2. And this is the microsoft's tool: https://learn.microsoft.com/en-us/azure/service-bus-messaging/explorer.