Skip to content

Files

Latest commit

cb9812b · Dec 15, 2021

History

History
153 lines (107 loc) · 6.74 KB

quickstart-azure-functions-csharp.md

File metadata and controls

153 lines (107 loc) · 6.74 KB
title description services author ms.service ms.devlang ms.custom ms.topic ms.date ms.author
Quickstart for Azure App Configuration with Azure Functions | Microsoft Docs
In this quickstart, make an Azure Functions app with Azure App Configuration and C#. Create and connect to an App Configuration store. Test the function locally.
azure-app-configuration
AlexandraKemperMS
azure-app-configuration
csharp
devx-track-csharp, mode-other
quickstart
06/02/2021
alkemper

Quickstart: Create an Azure Functions app with Azure App Configuration

In this quickstart, you incorporate the Azure App Configuration service into an Azure Functions app to centralize storage and management of all your application settings separate from your code.

Prerequisites

Create an App Configuration store

[!INCLUDE azure-app-configuration-create]

  1. Select Configuration Explorer > + Create > Key-value to add the following key-value pairs:

    Key Value
    TestApp:Settings:Message Data from Azure App Configuration

    Leave Label and Content Type empty for now.

  2. Select Apply.

Create a Functions app

[!INCLUDE Create a project using the Azure Functions template]

Connect to an App Configuration store

This project will use dependency injection in .NET Azure Functions and add Azure App Configuration as an extra configuration source.

  1. Right-click your project, and select Manage NuGet Packages. On the Browse tab, search for and add following NuGet packages to your project.

  2. Add a new file, Startup.cs, with the following code. It defines a class named Startup that implements the FunctionsStartup abstract class. An assembly attribute is used to specify the type name used during Azure Functions startup.

    The ConfigureAppConfiguration method is overridden and Azure App Configuration provider is added as an extra configuration source by calling AddAzureAppConfiguration(). The Configure method is left empty as you don't need to register any services at this point.

    using System;
    using Microsoft.Azure.Functions.Extensions.DependencyInjection;
    using Microsoft.Extensions.Configuration;
    
    [assembly: FunctionsStartup(typeof(FunctionApp.Startup))]
    
    namespace FunctionApp
    {
        class Startup : FunctionsStartup
        {
            public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
            {
                string cs = Environment.GetEnvironmentVariable("ConnectionString");
                builder.ConfigurationBuilder.AddAzureAppConfiguration(cs);
            }
    
            public override void Configure(IFunctionsHostBuilder builder)
            {
            }
        }
    }
  3. Open Function1.cs, and add the following namespace.

    using Microsoft.Extensions.Configuration;

    Add a constructor used to obtain an instance of IConfiguration through dependency injection.

    private readonly IConfiguration _configuration;
    
    public Function1(IConfiguration configuration)
    {
        _configuration = configuration;
    }
  4. Update the Run method to read values from the configuration.

    public async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");
    
        string keyName = "TestApp:Settings:Message";
        string message = _configuration[keyName];
    
        return message != null
            ? (ActionResult)new OkObjectResult(message)
            : new BadRequestObjectResult($"Please create a key-value with the key '{keyName}' in App Configuration.");
    }

    [!NOTE] The Function1 class and the Run method should not be static. Remove the static modifier if it was autogenerated.

Test the function locally

  1. Set an environment variable named ConnectionString, and set it to the access key to your App Configuration store. If you use the Windows command prompt, run the following command and restart the command prompt to allow the change to take effect:

        setx ConnectionString "connection-string-of-your-app-configuration-store"

    If you use Windows PowerShell, run the following command:

        $Env:ConnectionString = "connection-string-of-your-app-configuration-store"
    

    If you use macOS or Linux, run the following command:

        export ConnectionString='connection-string-of-your-app-configuration-store'
  2. Press F5 to test your function. If prompted, accept the request from Visual Studio to download and install Azure Functions Core (CLI) tools. You might also need to enable a firewall exception so that the tools can handle HTTP requests.

  3. Copy the URL of your function from the Azure Functions runtime output.

    Quickstart Function debugging in VS

  4. Paste the URL for the HTTP request into your browser's address bar. The following image shows the response in the browser to the local GET request returned by the function.

    Quickstart Function launch local

Clean up resources

[!INCLUDE azure-app-configuration-cleanup]

Next steps

In this quickstart, you created a new App Configuration store and used it with an Azure Functions app via the App Configuration provider. To learn how to update your Azure Functions app to dynamically refresh configuration, continue to the next tutorial.

[!div class="nextstepaction"] Enable dynamic configuration in Azure Functions