Redaguoti

Bendrinti naudojant


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

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

Prerequisites

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

Receive messages by using Logic Apps

In this step, you create an Azure logic app that receives Service Bus events via Azure Event Grid.

  1. Select + Create a resource, select Integration, and then select Logic App.

    Screenshot that shows the Create a resource -> Integration -> Logic app menu.

  2. On the Create Logic App page, follow these steps:

    1. Select your Azure subscription.
    2. Select Use existing for the Resource group, and select the resource group that you used for other resources (like Azure function, Service Bus namespace) that you created earlier.
    3. Enter a name for the logic app.
    4. Select the Region for the logic app.
    5. For Plan type, select Consumption.
    6. Select Review + Create. Screenshot that shows the Create a logic app page.
    7. On the Review + Create page, select Create to create the logic app.
  3. On the Deployment complete page, select Go to resource to navigate to the Logic app page.

  4. On the Logic Apps Designer page, select Blank Logic App under Templates.

Add a step receive messages from Service Bus via Event Grid

  1. On the Logic app page, select Logic app designer on the left menu.

  2. In the right pane, under Templates, select Blank Logic App.

    Screenshot that shows the Logic app designer page with the Blank logic app option selected.

  3. On the designer, do the following steps:

    1. Search for Event Grid.

    2. Select When a resource event occurs - Azure Event Grid.

      Screenshot that shows the Logic Apps Designer with Event Grid trigger selected.

  4. Select Sign in.

    Screenshot that shows the Logic Apps Designer with the Sign-in button selected.

  5. On the Sign in to your account page, select the account you want to use to sign in to Azure. 1.

  6. On the When a resource event occurs page, do the following steps:

    1. Select your Azure subscription.

    2. For Resource Type, select Microsoft.ServiceBus.Namespaces.

    3. For Resource Name, select your Service Bus namespace.

    4. Select Add new parameter, select Suffix Filter, and then move the focus outside drop-down list.

      Screenshot that shows adding of a new parameter of type Suffix filter.

    5. For Suffix Filter, enter the name of your Service Bus topic subscription.

      Screenshot that shows the Logic Apps Designer with connection configuration for the Service Bus namespace.

  7. Select + New Step in the designer, and do the following steps:

    1. Search for Service Bus.

    2. Select Service Bus in the list.

      Screenshot that shows the selection of Service Bus.

    3. Select for Get messages in the Actions list.

    4. Select Get messages from a topic subscription (peek-lock).

      Screenshot that shows the Logic Apps Designer with Get messages from a topic subscription selected.

    5. Follow these steps:

      1. Enter a name for the connection. For example: Get messages from the topic subscription.

      2. Confirm that Authentication Type is set to Access Key.

      3. For Connection String, copy and paste the connection string to the Service Bus namespace that you saved earlier.

      4. Select Create.

        Screenshot that shows the Logic Apps Designer with the Service Bus connection string specified.

    6. Select your topic and subscription.

      Screenshot that shows the Logic Apps Designer with the Service Bus topic and subscription specified.

Add a step to process and complete received messages

In this step, you add steps to send the received message in an email and then complete the message. In a real-world scenario, you process a message in the logic app before completing the message.

Add a foreach loop

  1. Select + New step.

  2. Search for and then select Control.

    Screenshot that shows the Control category.

  3. In the Actions list, select For each.

    Screenshot that shows the For-each operation selected.

  4. For Select an output from previous steps (click inside text box if needed), select Body under Get messages from a topic subscription (peek-lock).

    Screenshot that shows the selection of For each input.

Add a step inside the foreach loop to send an email with the message body

  1. Within For Each loop, select Add an action.

    Screenshot that shows the selection of Add an action button in the For-each loop.

  2. In the Search connectors and actions text box, enter Office 365.

  3. Select Office 365 Outlook in the search results.

    Screenshot that shows the selection of Office 365.

  4. In the list of actions, select Send an email (V2).

    Screenshot that shows the selection of Send an email operation.

  5. Select Sign in, and follow steps to create a connection to Office 365 Outlook.

  6. In the Send an email (V2) window, follow these steps:

  7. Select inside the text box for Body, and follow these steps:

    1. For To, enter an email address.

    2. For Subject, enter Message received from Service Bus topic's subscription.

    3. Switch to Expression.

    4. Enter the following expression:

      base64ToString(items('For_each')?['ContentData'])
      
    5. Select OK.

      Screenshot that shows the expression for Body of the Send an email activity.

Add another action in the foreach loop to complete the message

  1. Within For Each loop, select Add an action.

    1. Select Service Bus in the Recent list.

    2. Select Complete the message in a topic subscription from the list of actions.

      Screenshot that shows the selection of Complete a message in a topic subscription.

    3. Select your Service Bus topic.

    4. Select a subscription to the topic.

    5. For Lock token of the message, select Lock Token from the Dynamic content.

      Screenshot that shows the lock token field.

  2. Select Save on the toolbar on the Logic Apps Designer to save the logic app.

    Screenshot that shows the Save button in the Logic app designed.

Test the app

  1. If you haven't already sent test messages to the topic, follow instructions in the Send messages to the Service Bus topic section to send messages to the topic.

  2. Switch to the Overview page of your logic app and then switch to the Runs history tab in the bottom pane. You see the logic app runs messages that were sent to the topic. It could take a few minutes before you see the logic app runs. Select Refresh on the toolbar to refresh the page.

    Screenshot that shows the Logic app run history.

  3. Select a logic app run to see the details. Notice that it processed 5 messages in the for loop.

    Screenshot that shows the details for the selected logic app run.

  4. You should get an email for each message the logic app receives.

    Screenshot of Outlook with the messages received from the topics' subscription.

Troubleshoot

If you don't see any 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 once, so I see 5 messages.

    Screenshot that shows the Service Bus Topic page with incoming message count selected.

  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.

    Screenshot that shows the Service Bus Subscription page with the active message count selected.

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

    Screenshot that shows the Events page of the Service Bus Namespace page.

  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.

    Screenshot that shows the Event Subscription page with the delivered event count selected.

Next steps