Logic Apps, Service Bus and Send Grid

In my endeavor to get a better understanding of Azure Logic Apps, I converted some existing code in one of my side projects from Azure Storage Queues and Azure Functions to Service Bus and Logic Apps.

The code I was converting sends notifications to users. My code will send a message to a Service Bus queue, which will then be picked up by a Logic App, which will then send an email, via SendGrid, to the multiple recipients.

The following Azure resources are needed:

  • Service Bus
  • Service Bus Queue
  • SendGrid – The free tier works perfect, you will also need to create an API Key, which the Logic App will use

The following is the code I am using to send a message to the Service Bus queue:

var retryPolicy =
    new RetryExponential(TimeSpan.FromSeconds(0.1), TimeSpan.FromSeconds(30), 3);

var json =
    JsonConvert.SerializeObject(data);

var message = new Message(
    Encoding.UTF8.GetBytes(json));

message.ContentType = "application/json";

var queueClient = new QueueClient(
    _azureServiceBusOptions.Value.PrimaryConnectionString,
    type,
    retryPolicy: retryPolicy);

Step 1 – Create Logic App

Assuming you have created all the necessary Azure resources, you can now create your Logic App.

Step 2 – Add Service Bus Trigger

Add a trigger that reads a message from Service Bus. You will be prompted to create your connection first, followed by selecting your queue and setting the retrieval interval.

Step 3 – Convert Service Bus Message to JSON

The next step involved add a Parse JSON step to convert the content from a Base64 String to JSON. 

You will also want to paste in a sample of the JSON schema, this will allow further steps to access the properties.

Step 4 – Send Email

You now add a step to loop through each recipient and send the email message.

I apologize for the brevity of this post, it is more or less for my notes, but figured someone might get some help from it, thus the reason for sharing.

Leave a Reply

Your email address will not be published.