Overview:

Azure Functions lets you use Visual Studio to create local C# function projects and then easily publish this project to run in a scalable serverless environment in Azure. 

In this post, you will explore and see how to create C# functions that run on .NET 8 in an isolated worker process. Function apps that run in an isolated worker process are supported on all versions of .NET that are supported by Functions.

Demo-Summary

  1. Setting Up: Ensure you have Visual Studio 2022 with the Azure development workload installed.
  2. Creating a Function App Project: Use the Azure Functions project template in Visual Studio to create a C# class library project. This project can be published to a function app in Azure, which groups functions as a logical unit for management, deployment, and scaling.
  3. Developing the Function: The template allows you to create a function that responds to HTTP requests. You can write the function code in C#.
  4. Running Locally: Before deploying, you can run and test your function locally to verify its behavior.
  5. Deploying to Azure: Once you’re satisfied with the local version of your function, you can deploy your code project to Azure Functions.

Demo (Step-by-step)

Let’s create Azure Function Project.

SettingValueDescription
Functions worker.NET 8.0 Isolated (Long Term Support)Your functions run on .NET 8 in an isolated worker process.
FunctionHTTP triggerThis value creates a function triggered by an HTTP request.
Use Azurite for runtime storage account (AzureWebJobsStorage)EnableBecause a function app in Azure requires a storage account, one is assigned or created when you publish your project to Azure. An HTTP trigger doesn’t use an Azure Storage account connection string; all other trigger types require a valid Azure Storage account connection string. When you select this option, the Azurite emulator is used.
Authorization levelAnonymousThe created function can be triggered by any client without providing a key. This authorization setting makes it easy to test your new function..

What are the different types of Function Types?

Authorization Level:

Run the function locally

Changed to Hello world Type!!!!

 

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FunctionApp2
{
    public static class Function1
    {
        [FunctionName("Function1")]


        public static IActionResult Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
        {
            return new OkObjectResult("Welcome to Azure Functions!");
        }
    }
}

You hit the url and Azure Function is executed those many times !

Publish the project to Azure

 Visual Studio can publish your local project to Azure. Before you can publish your project, you must have a function app in your Azure subscription. If you don’t already have a function app in Azure, Visual Studio publishing creates one for you the first time you publish your project.

Option1: Publish from VS

SettingValueDescription
NameGlobally unique nameName that uniquely identifies your new function app.   Accept this name or enter a new name.   Valid characters are: a-z, 0-9, and -.
SubscriptionYour subscriptionThe Azure subscription to use.   Accept this subscription or select a new one from the drop-down list.
Resource groupName of your resource groupThe resource group in which you want to create your function app.   Select New to create a new resource group.   You can also choose an existing resource group from the drop-down list.
Plan TypeConsumptionWhen you publish your project to a function app that runs in a Consumption plan, you pay only for executions of your functions app.   Other hosting plans incur higher costs.
LocationLocation of the app serviceChoose a Location in a region near you or other services your functions access.
Azure StorageGeneral-purpose storage accountAn Azure storage account is required by the Functions runtime.   Select New to configure a general-purpose storage account. You can also choose an existing account that meets the storage account requirements.   WHY ? Azure Storage is required for Azure Function deployment because Azure Functions uses Azure Storage to store triggerslogs, and runtime information. Azure Storage also provides durability and scalability for Azure Functions.
Application InsightsApplication Insights instanceYou should enable Application Insights integration for your function app.   Select New to create a new instance, either in a new or in an existing Log Analytics workspace.     You can also choose an existing instance.

Option2: Create the Azure Function in Azure Portal

Hosting options:

Create Function App in Azure Portal

Hosting Options:

Verify your function in Azure

In the address bar in your browser, paste the URL you just copied and run the request.

The URL that calls your HTTP trigger function is in the following format:
http://<APP_NAME&gt;.azurewebsites.net/api/HttpExample?name=Functions

https://functionapp220240225185909.azurewebsites.net/api/Function1

Conclusion

Visual Studio streamlines the process of developing Azure Functions by providing a dedicated project template and integrated tools for local testing and deployment. This enables developers to quickly create, test, and deploy serverless functions to Azure, simplifying the development lifecycle and allowing for easy scaling and management of cloud resources. Whether you’re new to Azure Functions or an experienced developer, Visual Studio offers a robust environment for serverless application development.

 References:

Getting started with Azure Functions | Microsoft Learn

Quickstart: Create your first C# function in Azure using Visual Studio | Microsoft Learn

Leave a Reply

Discover more from Rajeev Singh | Coder, Blogger, YouTuber

Subscribe now to keep reading and get access to the full archive.

Continue reading