Tutorial: Respond to Azure Service Bus events received via Azure Event Grid by using Azure Functions

In this tutorial, you learn how to respond to Azure Service Bus events that are received via Azure Event Grid by using Azure Functions and Azure Logic Apps.

In this tutorial, you learn how to:

  • Create a Service Bus namespace
  • Prepare a sample application to send messages
  • Send messages to the Service Bus topic
  • Receive messages by using Logic Apps
  • Set up a test function on Azure
  • Connect the function and namespace via Event Grid
  • Receive messages by using Azure Functions


If you don't have an Azure subscription, create a free account before you begin.

Create a Service Bus namespace

Follow instructions in this tutorial: Quickstart: Use the Azure portal to create a Service Bus topic and subscriptions to the topic to do the following tasks:

  • Create a premium Service Bus namespace.
  • Get the connection string.
  • Create a Service Bus topic.
  • Create a subscription to the topic. You need only one subscription in this tutorial, so no need to create subscriptions S2 and S3.

Send messages to the Service Bus topic

In this step, you use a sample application to send messages to the Service Bus topic you created in the previous step.

  1. Clone the GitHub azure-service-bus repository or download the zip file and extract files from it.

  2. In Visual Studio, go to the \samples\DotNet\Azure.Messaging.ServiceBus\ServiceBusEventGridIntegrationV2 folder, and then open the SBEventGridIntegration.sln file.

  3. In the Solution Explorer window, expand the MessageSender project, and select Program.cs.

  4. Replace <SERVICE BUS NAMESPACE - CONNECTION STRING> with the connection string to your Service Bus namespace and <TOPIC NAME> with the name of the topic.

    const string ServiceBusConnectionString = "<SERVICE BUS NAMESPACE - CONNECTION STRING>";
    const string TopicName = "<TOPIC NAME>";
  5. Build and run the program to send 5 test messages (const int numberOfMessages = 5;) to the Service Bus topic.

    Console app output

Additional prerequisites

Install Visual Studio 2019 and include the Azure development workload. This workload includes Azure Function Tools that you need to create, build, and deploy Azure Functions projects in Visual Studio.

Deploy the function app


To learn more about creating and deploying an Azure Functions app, see Develop Azure Functions using Visual Studio

  1. Open ReceiveMessagesOnEvent.cs file from the FunctionApp1 project of the SBEventGridIntegration.sln solution.

  2. Replace <SERVICE BUS NAMESPACE - CONNECTION STRING> with the connection string to your Service Bus namespace. It should be the same as the one you used in the Program.cs file of the MessageSender project in the same solution.

  3. Right-click FunctionApp1, and select Publish.

  4. On the Publish page, select Start. These steps may be different from what you see, but the process of publishing should be similar.

  5. In the Publish wizard, on the Target page, select Azure for Target.

  6. On the Specific target page, select Azure Function App (Windows).

  7. On the Functions instance page, select Create a new Azure function.

    Image showing the Add function button of the Visual Studio - Publish dialog box.

  8. On the Function App (Windows) page, follow these steps:

    1. Enter a name for the function app.
    2. Select an Azure subscription.
    3. Select an existing resource group or create a new resource group. For this tutorial, select the resource group that has the Service Bus namespace.
    4. Select a plan type for App Service.
    5. Select a location. Select the same location as the Service Bus namespace.
    6. Select an existing Azure Storage or select New to create a new Storage account to be used by the Functions app.
    7. Select Create to create the Functions app.
  9. Back on the Functions instance page of the Publish wizard, select Finish.

  10. On the Publish page in Visual Studio, select Publish to publish the Functions app to Azure.

    Publish Functions app

  11. In the Output window, see the messages from build and publish, and confirm that they both succeeded.

  12. Now, on the Publish page, in the Hosting section, select ... (ellipsis), and select Manage in Azure portal.

  13. In the Azure portal, on the Function App page, select Functions in the left menu, and confirm that you see two functions:

    Screenshot showing the Functions page with the Event Grid Trigger function.

  14. Select EventGridTriggerFunction from the list. We recommend that you use the Event Grid trigger with Azure Functions as it has a few benefits over using the HTTP trigger. For details, see Azure function as an event handler for Event Grid events.

  15. On the Function page for the EventGridTriggerFunction, select Monitor on the left menu.

  16. Select Configure to configure Application Insights to capture invocation log. You use this page to monitor function executions upon receiving Service Bus events from Event Grid.

  17. On the Application Insights page, enter a name for the resource, select a location for the resource, and then select OK.

  18. Select the function EventGridTriggerFunction at the top (breadcrumb menu) to navigate back to the Function page.

  19. Confirm that you on the Monitor page.

    Monitor page for the function before function invocations

    Keep this page open in a tab your web browser. You'll refresh this page to see invocations for this function later.

Connect the function and the Service Bus namespace via Event Grid

In this section, you tie together the function and the Service Bus namespace by using the Azure portal.

To create an Azure Event Grid subscription, follow these steps:

  1. In the Azure portal, go to your Service Bus namespace and then, in the left pane, select Events. Your namespace window opens, with two Event Grid subscriptions displayed in the right pane.

    Service Bus - events page

  2. Select + Event Subscription on the toolbar.

  3. On the Create Event Subscription page, do the following steps:

    1. Enter a name for the subscription.

    2. Enter a name for the system topic. System topics are topics created for Azure resources such as Azure Storage account and Azure Service Bus. To learn more about system topics, see System topics overview.

    3. Select Azure Function for Endpoint Type, and click Select an endpoint.

      Service Bus - Event Grid subscription

    4. On the Select Azure Function page, select the subscription, resource group, function app, slot, and the function, and then select Confirm selection.

      Function - select the endpoint

    5. On the Create Event Subscription page, switch to the Filters tab, and do the following tasks:

      1. Select Enable subject filtering

      2. Enter the name of the subscription to the Service Bus topic (S1) you created earlier.

      3. Select the Create button.

        Event subscription filter

  4. Switch to the Event Subscriptions tab of the Events page and confirm that you see the event subscription in the list.

    Event subscription in the list

Monitor the Functions app

The messages you sent to the Service Bus topic earlier are forwarded to the subscription (S1). Event Grid forwards the messages at the subscription to the Azure function. In this step of the tutorial, you confirm the function was invoked and view the logged informational messages.

  1. On the page for your Azure function app, switch to the Monitor tab if it isn't already active. You should see an entry for each message posted to the Service Bus topic. If you don't see them, refresh the page after waiting for a few minutes.

    Monitor page for the function after invocations

  2. Select the invocation from the list to see the details.

    Function invocation details

    You can also use the Logs tab of the Monitor page to see the logging information as the messages are sent. There could some delay, so give it a few minutes to see the logged messages.

    Function logs


If you don't see any function invocations after waiting and refreshing for sometime, follow these steps:

  1. Confirm that the messages reached the Service Bus topic. See the incoming messages counter on the Service Bus Topic page. In this case, I ran the MessageSender application twice, so I see 10 messages (5 messages for each run).

    Service Bus Topic page - incoming messages

  2. Confirm that there are no active messages at the Service Bus subscription. If you don't see any events on this page, verify that the Service Bus Subscription page doesn't show any Active message count. If the number for this counter is greater than zero, the messages at the subscription aren't forwarded to the handler function (event subscription handler) for some reason. Verify that you've set up the event subscription properly.

    Active message count at the Service Bus subscription

  3. You also see delivered events on the Events page of the Service Bus namespace.

    Events page - delivered events

  4. You can also see that the events are delivered on the Event Subscription page. You can get to this page by selecting the event subscription on the Events page.

    Event subscription page - delivered events

Next steps