Adding to Queue Storage from Azure Function

In last article we created our first function. Function, provides wide variety of input and output connection without writing code, with declarative syntax. In this article, we are going to store messages in Azure Storage when function will be called with HttpTrigger.

Azure Queue Storage

Queue Storage delivers asynchronous messaging between application components, whether they are running in cloud, on the desktop, on an on-premise server, or on a mobile device. Azure Queue storage is a service for storing large numbers of messages that can be accessed from anywhere in the world with authenticated calls using HTTP or HTTPs. Single unique message can be up to 64kb in size and millions of messages, up to the total capicity limit of storage account. It used to create backlog of work to process asynchronously.

Queue Storage Component

It has mainly four component:

  • Url : queue storage is accessed via a url which looks like http://<storage account>.queue.core.winows.net/<queue>
  • Storage Account: it is created by default when you create function or any other service. It can be created separately.
  • Queue: We can create a queue within storage account.
  • Message: One queue contains many messages as long as it can fit into storage. Message can remain up to 7 days. if -1 specified than it doesn’t expire.

Create Function to add message to storage

I have created below sample HttpTrigger function and added Storage Queue input binding.

In above HttpTrigger Function, we have added new msg parameter of type [Queue("outqueue"),StorageAccount("AzureWebJobsStorage")] ICollector<string> msg, where “outqueue” is the name of the queue in “AzureWebJobStorage” StorageAccount. “AzureWebJobStorage” is application setting on the cloud as well “local.settings.json” file.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=storageaccountfuncrb9b0;AccountKey=LZdMUmleda4FlyP8e25ALF4UmZaDN1vygZWAw5J/kad/EkgRszwYTwngiB471qFc7M3PSLg96vsWimxfD+L2oA==;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "FUNCTIONS_EXTENSION_VERSION": "~3",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "84ceb92a-9c69-4fd7-90d2-0f6b10f7eb3c",
    "APPLICATIONINSIGHTS_CONNECTION_STRING": "InstrumentationKey=84ceb92a-9c69-4fd7-90d2-0f6b10f7eb3c",
    "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING": "DefaultEndpointsProtocol=https;AccountName=storageaccountfuncrb9b0;AccountKey=LZdMUmleda4FlyP8e25ALF4UmZaDN1vygZWAw5J/kad/EkgRszwYTwngiB471qFc7M3PSLg96vsWimxfD+L2oA==;EndpointSuffix=core.windows.net",
    "WEBSITE_CONTENTSHARE": "nps-funcb043",
    "WEBSITE_RUN_FROM_PACKAGE": "1",
    "WEBSITE_ENABLE_SYNC_UPDATE_SITE": "true"
  }
}

As I mentioned earlier, when you create the function storage account is created by default and added in application settings on cloud. To run the function locally, you have to download setting from the cloud. You can use command by pressing “F1” and type “Azure Fucntions: Download Remote Settings…

When you run function locally or from cloud TestMessage: [GET,POST] http://localhost:7071/api/TestMessage?name=TEST it’ll execute the function and store value in storage queue when execute msg.Add($"Value passed to function is :{name}"); statement.

Verify the Queue Storage

Above code won’t work until the queue is created. “outqueue” is custom queue we have to create in storage account. I have downloaded the storage explorer locally from https://docs.microsoft.com/en-us/azure/vs-azure-tools-storage-manage-with-storage-explorer?tabs=macos. You can use your azure account to connect to storage account. I’ll show you all subscription and resource within the resource. We will expand our target storage which we are connecting through code. There are by default 4 folder/section in the storage account. These are “Blob Containers”, File Shares, Queues and Tables. When we select the Queues, by default, intially, there is no queue created. We will create “outqueue” with Queues folder.

When we’ll call function with http://localhost:7071/api/TestMessage?name=TEST Url, it’ll add message “TEST” to the queue. See screenshot.

This message is added queue and process asynchronously and independently. It is a good example of microservice architecture, that we can program each component separately and scale up and down accordingly independently.