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 |
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.
- Azure subscription - create one for free
- Visual Studio 2019 with the Azure development workload.
- Azure Functions tools
[!INCLUDE azure-app-configuration-create]
-
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.
-
Select Apply.
[!INCLUDE Create a project using the Azure Functions template]
This project will use dependency injection in .NET Azure Functions and add Azure App Configuration as an extra configuration source.
-
Right-click your project, and select Manage NuGet Packages. On the Browse tab, search for and add following NuGet packages to your project.
- Microsoft.Extensions.Configuration.AzureAppConfiguration version 4.1.0 or later
- Microsoft.Azure.Functions.Extensions version 1.1.0 or later
-
Add a new file, Startup.cs, with the following code. It defines a class named
Startup
that implements theFunctionsStartup
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 callingAddAzureAppConfiguration()
. TheConfigure
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) { } } }
-
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; }
-
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 theRun
method should not be static. Remove thestatic
modifier if it was autogenerated.
-
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'
-
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.
-
Copy the URL of your function from the Azure Functions runtime output.
-
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.
[!INCLUDE azure-app-configuration-cleanup]
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