Skip to content

Commit b7ca8e2

Browse files
committed
last missing file
1 parent b6dcea2 commit b7ca8e2

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
---
2+
title: Tutorial for using Azure App Configuration dynamic configuration in an Azure Functions app | Microsoft Docs
3+
description: In this tutorial, you learn how to dynamically update the configuration data for Azure Functions apps
4+
services: azure-app-configuration
5+
documentationcenter: ''
6+
author: zhenlan
7+
manager: qingye
8+
editor: ''
9+
10+
ms.assetid:
11+
ms.service: azure-app-configuration
12+
ms.workload: tbd
13+
ms.devlang: csharp
14+
ms.topic: tutorial
15+
ms.date: 11/17/2019
16+
ms.author: zhenlwa
17+
ms.custom: azure-functions
18+
ms.tgt_pltfrm: Azure Functions
19+
20+
#Customer intent: I want to dynamically update my Azure Functions app to use the latest configuration data in App Configuration.
21+
---
22+
# Tutorial: Use dynamic configuration in an Azure Functions app
23+
24+
The App Configuration .NET Standard configuration provider supports caching and refreshing configuration dynamically driven by application activity. This tutorial shows how you can implement dynamic configuration updates in your code. It builds on the Azure Functions app introduced in the quickstarts. Before you continue, finish [Create an Azure functions app with Azure App Configuration](./quickstart-azure-functions-csharp.md) first.
25+
26+
In this tutorial, you learn how to:
27+
28+
> [!div class="checklist"]
29+
> * Set up your Azure Functions app to update its configuration in response to changes in an App Configuration store.
30+
> * Inject the latest configuration to your Azure Functions calls.
31+
32+
## Prerequisites
33+
34+
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
35+
- [Visual Studio 2019](https://visualstudio.microsoft.com/vs) with the **Azure development** workload
36+
- [Azure Functions tools](../azure-functions/functions-develop-vs.md#check-your-tools-version)
37+
- Finish quickstart [Create an Azure functions app with Azure App Configuration](./quickstart-azure-functions-csharp.md)
38+
39+
## Reload data from App Configuration
40+
41+
1. Open *Function1.cs*. In addition to the `static` property `Configuration`, add a new `static` property `ConfigurationRefresher` to keep a singleton instance of `IConfigurationRefresher` that will be used to signal configuration updates during Functions calls later.
42+
43+
```csharp
44+
private static IConfiguration Configuration { set; get; }
45+
private static IConfigurationRefresher ConfigurationRefresher { set; get; }
46+
```
47+
48+
2. Update the constructor and use the `ConfigureRefresh` method to specify the setting to be refreshed from the App Configuration store. An instance of `IConfigurationRefresher` is retrieved using `GetRefresher` method. Optionally, we also change the configuration cache expiration time window to 1 minute from the default 30 seconds.
49+
50+
```csharp
51+
static Function1()
52+
{
53+
var builder = new ConfigurationBuilder();
54+
builder.AddAzureAppConfiguration(options =>
55+
{
56+
options.Connect(Environment.GetEnvironmentVariable("ConnectionString"))
57+
.ConfigureRefresh(refreshOptions =>
58+
refreshOptions.Register("TestApp:Settings:Message")
59+
.SetCacheExpiration(TimeSpan.FromSeconds(60))
60+
);
61+
ConfigurationRefresher = options.GetRefresher();
62+
});
63+
Configuration = builder.Build();
64+
}
65+
```
66+
67+
3. Update the `Run` method and signal to refresh the configuration using the `Refresh` method at the beginning of the Functions call. This will be no-op if the cache expiration time window isn't reached. Remove the `await` operator if you prefer the configuration to be refreshed without blocking.
68+
69+
```csharp
70+
public static async Task<IActionResult> Run(
71+
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
72+
{
73+
log.LogInformation("C# HTTP trigger function processed a request.");
74+
75+
await ConfigurationRefresher.Refresh();
76+
77+
string keyName = "TestApp:Settings:Message";
78+
string message = Configuration[keyName];
79+
80+
return message != null
81+
? (ActionResult)new OkObjectResult(message)
82+
: new BadRequestObjectResult($"Please create a key-value with the key '{keyName}' in App Configuration.");
83+
}
84+
```
85+
86+
## Test the function locally
87+
88+
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:
89+
90+
setx ConnectionString "connection-string-of-your-app-configuration-store"
91+
92+
If you use Windows PowerShell, run the following command:
93+
94+
$Env:ConnectionString = "connection-string-of-your-app-configuration-store"
95+
96+
If you use macOS or Linux, run the following command:
97+
98+
export ConnectionString='connection-string-of-your-app-configuration-store'
99+
100+
2. To test your function, press F5. 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.
101+
102+
3. Copy the URL of your function from the Azure Functions runtime output.
103+
104+
![Quickstart Function debugging in VS](./media/quickstarts/function-visual-studio-debugging.png)
105+
106+
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.
107+
108+
![Quickstart Function launch local](./media/quickstarts/dotnet-core-function-launch-local.png)
109+
110+
5. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and select the App Configuration store instance that you created in the quickstart.
111+
112+
6. Select **Configuration Explorer**, and update the values of the following key:
113+
114+
| Key | Value |
115+
|---|---|
116+
| TestApp:Settings:Message | Data from Azure App Configuration - Updated |
117+
118+
7. Refresh the browser a few times. When the cached setting expires after a minute, the page shows the response of the Functions call with updated value.
119+
120+
![Quickstart Function refresh local](./media/quickstarts/dotnet-core-function-refresh-local.png)
121+
122+
The example code used in this tutorial can be downloaded from [App Configuration GitHub repo](https://github.com/Azure/AppConfiguration/tree/master/examples/DotNetCore/AzureFunction)
123+
124+
## Clean up resources
125+
126+
[!INCLUDE [azure-app-configuration-cleanup](../../includes/azure-app-configuration-cleanup.md)]
127+
128+
## Next steps
129+
130+
In this tutorial, you enabled your Azure Functions app to dynamically refresh configuration settings from App Configuration. To learn how to use an Azure managed identity to streamline the access to App Configuration, continue to the next tutorial.
131+
132+
> [!div class="nextstepaction"]
133+
> [Managed identity integration](./howto-integrate-azure-managed-service-identity.md)

0 commit comments

Comments
 (0)