Create and Deploy an Azure Function Using the Azure Portal

Azure Functions is a serverless compute service that enables developers to execute code in response to events without managing the underlying server infrastructure. It supports C# and is commonly used to build APIs, automate tasks, process data, and integrate applications with other Azure services.

In this article, we will learn how to create an Azure Function in C# using the Azure portal. We will create a Function App, implement an HTTP-triggered function, test its execution, and retrieve its URL to send HTTP requests.

By the end of this tutorial, you will have a basic understanding of how to create and test an Azure Function without relying on local development tools, where the selected runtime and hosting configuration support in-browser development.

Getting Started

Microsoft Azure provides a web-based portal that allows developers to create and manage Azure resources directly from a browser. Depending on the runtime and hosting configuration, developers can also create and test certain Azure Functions directly in the portal without installing development tools such as Visual Studio or Visual Studio Code.

Prerequisites

Before creating an Azure Function in C# using the Azure portal, make sure you have the following:

  • Azure subscription: An active Azure subscription to create and deploy Azure resources.
  • Azure account: Access to the Azure portal with the necessary permissions to create a Function App.
  • Required Azure permissions: Your Azure account must have sufficient permissions to create a Function App and its associated resources. The Contributor role at the resource group or subscription level generally provides the necessary permissions to create and manage resources within its scope. Additional permissions may be required to assign access roles or configure certain integrations.
  • Basic knowledge of C#: Familiarity with C# syntax, methods, and basic programming concepts.
  • Understanding of HTTP: Basic knowledge of HTTP requests and responses, as this tutorial uses an HTTP trigger.

Note: The ability to write and edit function code directly in the Azure portal depends on the selected runtime and hosting configuration. Some modern .NET configurations require developing the function locally and publishing it to Azure.

Create a Function App in the Azure Portal

An Azure Function App is a resource that provides the environment for running and managing one or more Azure Functions. Before creating an HTTP-triggered function, we need to create a Function App and configure its runtime and hosting settings.

Follow these steps to create a Function App:

Step 1: Open the Azure Portal
  • Sign in to the Azure portal using your Azure account.
  • In the search bar, search for Function App and select it from the available services. Click Create to start configuring a new Function App.

Step 2: Configure the Basic Settings
On the creation page, provide the following details:
  • Subscription: Select the Azure subscription you want to use.
  • Resource Group: Select an existing resource group or create a new one to organize the resources for this tutorial.
  • Function App name: Enter a globally unique name for your Function App. This name forms part of its default URL.
  • Runtime stack: Select .NET for developing functions in C#.
  • Version: Choose a supported .NET version that is compatible with your intended programming model.
  • Region: Select an Azure region close to your users or other application resources.

The available options may vary depending on the hosting plan and the current Azure configuration.

Step 3: Select the Hosting Plan

Choose a hosting plan based on your application's requirements. Azure Functions offers options such as the Flex Consumption, Premium, and Dedicated plans, with availability and features varying by region and configuration.

For a learning project, a consumption-based option is often a reasonable starting point because it can scale with demand. Review the pricing and execution limits before selecting a plan.

Step 4: Configure Storage

Azure Functions requires a storage account for runtime operations and other hosting-related features.

On the storage configuration page, select an existing storage account or create a new one, depending on the options presented. Ensure that the account meets the requirements of your selected hosting plan and runtime.

Step 5: Review and Create

Review the configuration to ensure that the subscription, resource group, application name, runtime, region, hosting plan, and storage settings are correct.

Click Review + create. After Azure validates the configuration, click Create to deploy the Function App.

Wait for the deployment to complete, then select Go to resource to open the newly created Function App.

Step 6: Verify the Function App

Once the resource is created, its Overview page displays information such as the application name, status, and default domain.

At this point, the Function App is ready for the next step, subject to its runtime and deployment configuration. We can now proceed to create an HTTP-triggered function.

Note: Creating a Function App does not automatically create a function. The next step is to add or publish a function to the app. Direct code creation in the Azure portal is not supported for every modern .NET runtime and hosting configuration.

Create an HTTP-Triggered Azure Function

An HTTP trigger allows an Azure Function to execute when it receives an HTTP request. It is commonly used to build REST APIs, webhooks, and lightweight endpoints that can be called by web applications or other services.

Step 1: Add an HTTP-Triggered Function

If your selected runtime and hosting configuration support creating functions directly in the Azure portal, follow these steps:

  1. Open your Function App in the Azure portal.
  2. Navigate to Functions and select Create.
  3. Choose HTTP trigger as the function template.
  4. Enter a name for the function, such as HelloFunction.
  5. Select an appropriate authorization level. For initial testing, Anonymous allows requests without a function key, while Function requires a valid key.
  6. Click Create to create the function.
The function should now appear under the Functions section of your Function App.

Note: These portal-based creation steps are not available for every C# runtime and hosting configuration. For new applications using the .NET isolated worker model, create the HTTP-triggered function in a local project and publish it to Azure.

Step 2: Configure the Function's Authorization

Azure Functions supports different authorization levels for HTTP-triggered functions:

  • Anonymous: Requests do not require a function key.
  • Function: Requests must include a valid function key.
  • Admin: Requests require the master key, which grants elevated access.

For a publicly accessible production endpoint, do not rely on anonymous access alone when authentication or authorization is required. Consider an appropriate authentication mechanism and protect all keys and credentials.

Understand How the HTTP Trigger Works

When an HTTP request reaches the function's endpoint, Azure Functions invokes the function code. The function processes the request and returns an HTTP response to the caller.

For example, a request might include a name as a query parameter:
</> html
https://your-function-app.azurewebsites.net/api/HelloFunction?name=Kailash
The function can read the name parameter and return a greeting such as Hello, Kailash!.
The default endpoint format is:
</> html
https://<APP_NAME>.azurewebsites.net/api/<FUNCTION_NAME>

Here, <APP_NAME> represents your Function App name, and <FUNCTION_NAME> represents the name of your function. The endpoint can differ if you configure a custom route or domain.

In the next section, we will implement the function logic in C# and return a response based on the incoming HTTP request. To learn more about Azure Function triggers, refer to my previous article.

Implement the Function in C#

Now that we have created the Function App and understood how an HTTP trigger works, let's implement a simple Azure Function in C#. In this example, we will create a function named HelloFunction that accepts a name through a query string and returns a greeting message.

For this implementation, we will use the .NET isolated worker model, which is recommended for new C# Azure Functions applications.

Write the Function Logic

Create a C# class named HelloFunction.cs in your Azure Functions project and add the following code:

</> C#
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Extensions.Logging;
using System.Net;
public class HelloFunction
{
  private readonly ILogger<HelloFunction> _logger;
  public HelloFunction(ILogger<HelloFunction> logger)
  {
    _logger = logger;
  }
  [Function("HelloFunction")]
  public HttpResponseData Run(
  [HttpTrigger(AuthorizationLevel.Function, "get")]
  HttpRequestData req)
  {
    _logger.LogInformation(
    "HelloFunction received an HTTP request.");
    string? name = System.Web.HttpUtility
    .ParseQueryString(req.Url.Query)["name"];
    var response = req.CreateResponse();
    if (string.IsNullOrWhiteSpace(name))
    {
      response.StatusCode = HttpStatusCode.BadRequest;
      response.WriteString(
      "Please provide a name in the query string.");
      return response;
    }
    response.StatusCode = HttpStatusCode.OK;
    response.WriteString($"Hello, {name}!");
    return response;
  }
}

This example uses the built-in HTTP model for the isolated worker. The project must reference the appropriate Azure Functions Worker and HTTP extension packages.

Understand the Code
  • [Function("HelloFunction")]: Identifies the method as an Azure Function and assigns it the name HelloFunction.
  • [HttpTrigger(...)]: Configures the function to execute when an HTTP GET request is received. The Function authorization level requires a valid function key.
  • HttpRequestData: Provides access to the incoming HTTP request, including its URL and query string.
  • Query string parameter: The code reads the name parameter from the request URL.
  • ILogger<HelloFunction>: Records diagnostic information that can help with troubleshooting.
  • HttpResponseData: Represents the HTTP response returned to the caller.
  • HTTP status codes: The function returns 200 OK when a name is supplied and 400 Bad Request when it is missing.
Example Request and Response

After publishing the function to Azure, call its endpoint using a URL in the following format:
</> html
https://your-function-app.azurewebsites.net/api/HelloFunction?name=Kailash

Expected response:
</> Plain Text
Hello, Kailash!
If the request does not include the name parameter, the function returns a 400 Bad Request response with the message:
</> Plain Text
Please provide a name in the query string.

Important: Because this code uses the isolated worker model, develop and build it in a local .NET project before publishing it to Azure. The Azure portal does not provide the same in-browser C# editing experience for this model as it does for legacy C# script functions.

This simple example demonstrates how to read request data, execute C# logic, and return an HTTP response from an Azure Function. In the next section, we will test the function to Azure to verify that it works as expected.

Test the Azure Function

After creating the C# function to Azure, we need to verify that it processes HTTP requests correctly and returns the expected responses. Azure Functions provides a Test/Run interface in the Azure portal that allows you to test a function without using an external HTTP client.

  1. Open the function in the Azure portal.
  2. Select Get function URL.
  3. Copy the URL, including the function key if one is required.
  4. Open a new browser tab and paste the URL into the address bar.
  5. Add the appropriate query parameter, such as name=Kailash, and press Enter.
For example:
</> html
https://your-function-app.azurewebsites.net/api/HelloFunction?name=Kailash

After creating the project, you will find several important files.
FilePurpose
run.csxContains the C# script code that implements the function logic.
function.jsonDefines the trigger and binding configuration, including the HTTP method and authorization level.
host.jsonContains configuration settings shared by the Function App.
local.settings.jsonStores local development settings; it is not normally generated or used as a local file when creating a function only in the portal.

Summary

In this article, we learned how to create an Azure Function in C# using the Azure portal. We explored how to configure a Function App, create an HTTP-triggered function, implement the function logic in C#, and test the endpoint to verify its response.

Azure Functions provides a convenient way to build serverless applications without managing the underlying server infrastructure. By combining C# with Azure Functions, developers can build lightweight APIs, automate tasks, and integrate applications with other Azure services.

As a next step, you can explore other trigger types, such as Timer, Blob Storage, and Queue Storage, to understand how Azure Functions can respond to different events and automate a wider range of workloads.

Thanks

Kailash Chandra Behera

I am an IT professional with over 12 years of experience in the full software development life cycle for Windows, services, and web-based applications using Microsoft .NET technologies.

Previous Post Next Post

نموذج الاتصال