Skip to content

Commit 438adb2

Browse files
Merge pull request #205714 from zhenlan/af-iso
Add isolated process for Azure Functions
2 parents 1a492d8 + 89c646e commit 438adb2

File tree

1 file changed

+86
-11
lines changed

1 file changed

+86
-11
lines changed

articles/azure-app-configuration/quickstart-azure-functions-csharp.md

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
title: Quickstart for Azure App Configuration with Azure Functions | Microsoft Docs
33
description: "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."
44
services: azure-app-configuration
5-
author: AlexandraKemperMS
5+
author: zhenlan
66
ms.service: azure-app-configuration
77
ms.devlang: csharp
88
ms.custom: devx-track-csharp, mode-other
99
ms.topic: quickstart
1010
ms.date: 06/02/2021
11-
ms.author: alkemper
11+
ms.author: zhenlwa
1212
#Customer intent: As an Azure Functions developer, I want to manage all my app settings in one place using Azure App Configuration.
1313
---
1414
# Quickstart: Create an Azure Functions app with Azure App Configuration
@@ -17,9 +17,9 @@ In this quickstart, you incorporate the Azure App Configuration service into an
1717

1818
## Prerequisites
1919

20-
- Azure subscription - [create one for free](https://azure.microsoft.com/free/dotnet)
21-
- [Visual Studio 2019](https://visualstudio.microsoft.com/vs) with the **Azure development** workload.
22-
- [Azure Functions tools](../azure-functions/functions-develop-vs.md#check-your-tools-version)
20+
- Azure subscription - [create one for free](https://azure.microsoft.com/free/dotnet).
21+
- [Visual Studio](https://visualstudio.microsoft.com/vs) with the **Azure development** workload.
22+
- [Azure Functions tools](../azure-functions/functions-develop-vs.md), if you don't have it installed with Visual Studio already.
2323

2424
## Create an App Configuration store
2525

@@ -40,13 +40,24 @@ In this quickstart, you incorporate the Azure App Configuration service into an
4040
[!INCLUDE [Create a project using the Azure Functions template](../../includes/functions-vstools-create.md)]
4141

4242
## Connect to an App Configuration store
43-
This project will use [dependency injection in .NET Azure Functions](../azure-functions/functions-dotnet-dependency-injection.md) and add Azure App Configuration as an extra configuration source.
43+
This project will use [dependency injection in .NET Azure Functions](/azure/azure-functions/functions-dotnet-dependency-injection) and add Azure App Configuration as an extra configuration source. Azure Functions support running [in-process](/azure/azure-functions/functions-dotnet-class-library) or [isolated-process](/azure/azure-functions/dotnet-isolated-process-guide). Pick the one that matches your requirements.
4444

4545
1. Right-click your project, and select **Manage NuGet Packages**. On the **Browse** tab, search for and add following NuGet packages to your project.
46-
- [Microsoft.Extensions.Configuration.AzureAppConfiguration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.AzureAppConfiguration/) version 4.1.0 or later
47-
- [Microsoft.Azure.Functions.Extensions](https://www.nuget.org/packages/Microsoft.Azure.Functions.Extensions/) version 1.1.0 or later
46+
### [In-process](#tab/in-process)
4847

49-
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.
48+
- [Microsoft.Extensions.Configuration.AzureAppConfiguration](https://www.nuget.org/packages/Microsoft.Extensions.Configuration.AzureAppConfiguration/) version 4.1.0 or later
49+
- [Microsoft.Azure.Functions.Extensions](https://www.nuget.org/packages/Microsoft.Azure.Functions.Extensions/) version 1.1.0 or later
50+
51+
### [Isolated process](#tab/isolated-process)
52+
53+
- [Microsoft.Azure.AppConfiguration.Functions.Worker](https://www.nuget.org/packages/Microsoft.Azure.AppConfiguration.Functions.Worker)
54+
55+
---
56+
57+
2. Add code to connect to Azure App Configuration.
58+
### [In-process](#tab/in-process)
59+
60+
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.
5061

5162
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.
5263

@@ -74,13 +85,35 @@ This project will use [dependency injection in .NET Azure Functions](../azure-fu
7485
}
7586
```
7687

77-
3. Open *Function1.cs*, and add the following namespace.
88+
### [Isolated process](#tab/isolated-process)
89+
90+
Open *Program.cs* and update the `Main()` method as following. You add Azure App Configuration provider as an extra configuration source by calling `AddAzureAppConfiguration()`.
91+
92+
```csharp
93+
public static void Main()
94+
{
95+
var host = new HostBuilder()
96+
.ConfigureAppConfiguration(builder =>
97+
{
98+
string cs = Environment.GetEnvironmentVariable("ConnectionString");
99+
builder.AddAzureAppConfiguration(cs);
100+
})
101+
.ConfigureFunctionsWorkerDefaults()
102+
.Build();
103+
104+
host.Run();
105+
}
106+
```
107+
---
108+
109+
3. Open *Function1.cs*, and add the following namespace if it's not present already.
78110

79111
```csharp
80112
using Microsoft.Extensions.Configuration;
81113
```
82114

83-
Add a constructor used to obtain an instance of `IConfiguration` through dependency injection.
115+
Add or update the constructor used to obtain an instance of `IConfiguration` through dependency injection.
116+
### [In-process](#tab/in-process)
84117

85118
```csharp
86119
private readonly IConfiguration _configuration;
@@ -91,14 +124,29 @@ This project will use [dependency injection in .NET Azure Functions](../azure-fu
91124
}
92125
```
93126

127+
### [Isolated process](#tab/isolated-process)
128+
```csharp
129+
private readonly IConfiguration _configuration;
130+
131+
public Function1(ILoggerFactory loggerFactory, IConfiguration configuration)
132+
{
133+
_logger = loggerFactory.CreateLogger<Function1>();
134+
_configuration = configuration;
135+
}
136+
```
137+
---
138+
94139
4. Update the `Run` method to read values from the configuration.
140+
### [In-process](#tab/in-process)
95141

96142
```csharp
143+
[FunctionName("Function1")]
97144
public async Task<IActionResult> Run(
98145
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
99146
{
100147
log.LogInformation("C# HTTP trigger function processed a request.");
101148

149+
// Read configuration data
102150
string keyName = "TestApp:Settings:Message";
103151
string message = _configuration[keyName];
104152

@@ -111,6 +159,28 @@ This project will use [dependency injection in .NET Azure Functions](../azure-fu
111159
> [!NOTE]
112160
> The `Function1` class and the `Run` method should not be static. Remove the `static` modifier if it was autogenerated.
113161

162+
### [Isolated process](#tab/isolated-process)
163+
164+
```csharp
165+
[Function("Function1")]
166+
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
167+
{
168+
_logger.LogInformation("C# HTTP trigger function processed a request.");
169+
170+
var response = req.CreateResponse(HttpStatusCode.OK);
171+
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
172+
173+
// Read configuration data
174+
string keyName = "TestApp:Settings:Message";
175+
string message = _configuration[keyName];
176+
177+
response.WriteString(message ?? $"Please create a key-value with the key '{keyName}' in Azure App Configuration.");
178+
179+
return response;
180+
}
181+
```
182+
---
183+
114184
## Test the function locally
115185

116186
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:
@@ -151,3 +221,8 @@ In this quickstart, you created a new App Configuration store and used it with a
151221

152222
> [!div class="nextstepaction"]
153223
> [Enable dynamic configuration in Azure Functions](./enable-dynamic-configuration-azure-functions-csharp.md)
224+
225+
To learn how to use an Azure managed identity to streamline the access to App Configuration, continue to the next tutorial.
226+
227+
> [!div class="nextstepaction"]
228+
> [Access App Configuration using managed identity](./howto-integrate-azure-managed-service-identity.md)

0 commit comments

Comments
 (0)