In this post, we will see how to create a simple Azure Functions that read an Azure Service Bus Queue, using Visual Studio.
First of all, we go to Azure Portal and we define a Service Bus (called postazurefunctions), where we will create a queue called queueone:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/0-1024x352.png)
Now, we open Visual Studio and we create an Azure Functions project:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/1-1024x683.png)
Then, we choose a “Trigger” that we want to use:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/2-1024x716.png)
and finally, we have to choose the Storage Account, the Queue connection string and the Queue name:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/3-1024x707.png)
In the end, this will be our project:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/Screenshot-2021-05-05-222500.png)
The first thing to do, is to insert the Queue Connection string in local.settings.json and use this parameter into Functions1.cs:
[LOCAL.SETTINGS.JSON]
{
"IsEncrypted": false,
"Values":
{
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=postforfunctions;AccountKey=f9323THLPdQ7l8SSqnRwo+jb0fcBxIZchSTXf9FESf2XET72fY3vzeqPeaRqdvK3TRelZzJrAa2wmDD2tdv8ow==;BlobEndpoint=https://postforfunctions.blob.core.windows.net/;TableEndpoint=https://postforfunctions.table.core.windows.net/;QueueEndpoint=https://postforfunctions.queue.core.windows.net/;FileEndpoint=https://postforfunctions.file.core.windows.net/",
"queueConnString": "Endpoint=sb://postazurefunctions.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=4kr1CM3nN/h84nK5IvUeGC/+L3Eill2DFlatP8UFV5Q=",
"FUNCTIONS_WORKER_RUNTIME": "dotnet"
}
}
[FUNCTION1.CS]
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;
namespace FunctionReadQueue
{
public static class Function1
{
[FunctionName("ReadQueue")]
public static void Run([ServiceBusTrigger("queueone", Connection = "queueConnString")]string myQueueItem, ILogger log)
{
if(string.Equals(myQueueItem,"Damiano"))
{
log.LogInformation($"Find the correct value ({myQueueItem}) at {DateTime.Now.ToString()}");
}
else
{
log.LogInformation($"Read item: {myQueueItem}");
}
}
}
}
Now, in order to check it works fine, we run the Functions and we will write two messages in the queue:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/demo1-1024x387.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/demo2-1024x517.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/demo3-1024x559.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/Screenshot-2021-05-05-225733-1024x372.png)
It works fine and now, we will deploy it on Azure.
In Solution Explorer, right-click the project and select Publish, the we choose Create New and finally we select Publish:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/4.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/5.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/6.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/7.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/8-1024x539.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/9-1024x470.png)
Now, if we go to Azure portal, we will see the new Azure Functions:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/10-1024x353.png)
Before testing the Function, we have to add the Queue Connections String in the Configuration Settings:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/11-1024x465.png)
Now, we can check the Azure Function:
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/12-1024x408.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/13-1024x465.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/14-1024x551.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/15-1024x546.png)
![](https://www.zoneofdevelopment.com/wp-content/uploads/2021/05/Screenshot-2021-05-05-230131-1024x511.png)
THIS IS A VERY SIMPLE EXAMPLE OF HOW TO USE AN AZURE FUNCTION BUT, LIKE THE POST “Azure – Functions with Azure Portal”, I WANTED JUST TO SHOW HOW IS EASY CREATE AN AZURE FUNCTION.