Date:

Share:

Azure Functions ⚡ For The .NET Developer: How To Easily Get Started

Related Articles

October 21, 20213 Reading minutes

What is the most essential part of programming?

– Yes, you guessed it – The code!

Azure Functions provides serverless computing for Azure. This allows us to focus on the most important pieces of code and let Azure handle the infrastructure and other aspects of code execution. This means that we as programmers have fewer things to maintain and manage, which often means less cost.

In this blog post, let’s write our first Azure function and learn the key concepts. We will write an Azure function to process messages that reach the Azure Storage Queue automatically.

Azure Functions can be used to build web APIs, respond to database changes, process streams, manage message queues and much more.

If you prefer to watch together while you read, check out my video here 👇

Writing our first function

Before you move on, make sure you have the Azure Functions core tool installed.

Azure Functions Core Tools help develop and test functions on the local computer using the full runtime of Functions. Depending on the IDE you use to write code, you can choose different ways to create a function – The command line, VS code, Visual studio, rider and so ‘

Creating functions involves selecting a template for the Function Trigger.


When creating the Azure function, we must specify a storage account (AzureWebJobsStorage). In the example above, it is configured for the storage emulator. When we deploy the function to the Azure infrastructure, it should be a real Azure Storage account that supports Blob, Queue and Table storage.

This storage account manages triggers, function registration performance, and other metadata of the Azure function itself. Here are some considerations You must perform if you reuse an existing storage account to activate a function.

God AzureWebJobsStorage A storage account is not related to the account that the function listens to messages in the queue. This queue / storage is identified by the two values ​​below AzureWebJobsStorage Definition in the dialog box / diagram above.

Triggers

Triggers run Azure Functions.

It defines how / when a function is activated. Each function must have exactly one trigger. Depending on the trigger, it has associated data transmitted in the Azure function as a charger.

In this example, we want to process messages that reach Azure Queue Storage. The queue activation template helps to define the initial code required to do so.

Function code

The Azure function is defined as a static method within a class library project identified by FunctionName attribute. This method is the entry point of the function. The method must define a Trigger attribute that specifies the type of trigger and links input data.

The function below is a function enabled by Azure Queue Storage. It works every time a message is sent in add-weatherdata. The queue connection string is specified as part of the environment configuration with the name WeatherDataQueue.

public static class Function1

    [FunctionName("ProcessWeatherData")]
    public static void Run(
        [QueueTrigger("add-weatherdata", Connection = "WeatherDataQueue")]string myQueueItem, 
        ILogger log)
    
        log.LogInformation($"C# Queue trigger function processed: myQueueItem");
    

The connection string WeatherDataQueue Can be specified as part of what local.settings.json File when running on the local development computer.


  "IsEncrypted": false,
  "Values": 
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "WeatherDataQueue": "<Connection String to Azure Storage Account>"
  

Launch the application after setting the correct connection string. If you release messages to the queue either through the Azure Portal or using code, it will be automatically captured in the function code and process.

In this case, he simply records the message.


Azure Function Local Application Console

Azure Function & Managed Identity

Azure Functions supports Managed Identities.

This means we can avoid having to insert connection strings to connect to other resources (Managed Identities Support) and instead allow Azure to manage the credentials for us.

Want to learn more about DefaultAzureCredential?

In the past, Azure had different ways to validate with different resources. The Azure SDK brings it all under one roof and provides more unified access to developers when connecting to resources in Azure.

In our example, we added the WeatherDataQueue Connection string to connect to Azure Queue Storage. We can replace it to use Managed Identites.

To enable managed identities for QueueTrigger we need to update it to the latest Nuget package (Extension Version 5.0.0-beta1 or later).

With the updated packages, update the name of the connection string b local.settings.json File with extension __queueServiceUri.


  "IsEncrypted": false,
  "Values": 
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "WeatherDataQueue__queueServiceUri": "https://youtubevideodemo.queue.core.windows.net/"
  

God QueueTrigger The class is set to use Managed Identity to connect to the storage account when it recognizes a connection string value attached to serviceUri.

Be sure to provide the identity that the app is running under the appropriate permissions in the Azure Storage account.

When running on the local computer, it can automatically pick up the user connected to Visual Studio. If the function is running on the Azure infrastructure, run Identity on the resource and grant permissions.


Assigning an Azure Function role for a managed identity

Since we need to access queue storage to read, write and delete messages, I have assigned the ‘Donates storage queue data‘Role for the appropriate identities as shown above.

The app does not use secrets or sensitive information to connect the queue storage. The Azure infrastructure manages this for us automatically. It’s one less thing to worry about for us developers.

Deploying Azure functions

The easiest way to deploy (for testing purposes) is to publish from the IDE. In Visual Studio, it’s built-in, for other IDEs you’ll need to install the Azure Tools extension.


Deploy the Azure function from Visual Studio

For real-world applications, I suggest setting up a construction-layout pipeline. If you are using Azure DevOps, check out the video below 👇

We have successfully created the Azure function, configured it to process messages from Queue Storage, and have an end-to-end structure and layout to push our code into the Azure infrastructure.

Whenever we change our function and commit it to our git database, it will be automatically deployed to the appropriate environment.

I hope this helped you understand and write your first Azure function ⚡

Buy me coffee

Source

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Popular Articles