Skip to content

Commit 6414692

Browse files
add .NET example
1 parent 853894b commit 6414692

File tree

4 files changed

+155
-22
lines changed

4 files changed

+155
-22
lines changed

articles/azure-app-configuration/TOC.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
items:
3434
- name: ASP.NET Core
3535
href: quickstart-feature-flag-aspnet-core.md
36-
- name: .NET Framework
36+
- name: .NET/.NET Framework
3737
href: quickstart-feature-flag-dotnet.md
3838
- name: Java Spring
3939
href: quickstart-feature-flag-spring-boot.md
14.9 KB
Loading
-6.04 KB
Loading

articles/azure-app-configuration/quickstart-feature-flag-dotnet.md

Lines changed: 154 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
---
2-
title: Quickstart for adding feature flags to .NET Framework apps | Microsoft Docs | Microsoft Docs
3-
description: A quickstart for adding feature flags to .NET Framework apps and managing them in Azure App Configuration
2+
title: Quickstart for adding feature flags to .NET apps
3+
titleSuffix: Azure App Configuration
4+
description: A quickstart for adding feature flags to .NET apps and managing them in Azure App Configuration.
45
services: azure-app-configuration
56
author: maud-lv
67
ms.service: azure-app-configuration
78
ms.devlang: csharp
89
ms.custom: devx-track-csharp, mode-other, devx-track-dotnet
910
ms.topic: quickstart
1011
ms.tgt_pltfrm: .NET
11-
ms.date: 3/20/2023
12-
ms.author: malev
13-
#Customer intent: As a .NET Framework developer, I want to use feature flags to control feature availability quickly and confidently.
12+
ms.date: 2/19/2024
13+
ms.author: zhiyuanliang
14+
#Customer intent: As a .NET developer, I want to use feature flags to control feature availability quickly and confidently.
1415
---
15-
# Quickstart: Add feature flags to a .NET Framework console app
16+
# Quickstart: Add feature flags to a .NET/.NET Framework console app
1617

17-
In this quickstart, you incorporate Azure App Configuration into a .NET Framework app to create an end-to-end implementation of feature management. You can use the App Configuration service to centrally store all your feature flags and control their states.
18+
In this quickstart, you incorporate Azure App Configuration into a .NET console app to create an end-to-end implementation of feature management. You can use the App Configuration service to centrally store all your feature flags and control their states.
1819

1920
The .NET Feature Management libraries extend the framework with feature flag support. These libraries are built on top of the .NET configuration system. They integrate with App Configuration through its .NET configuration provider.
2021

2122
## Prerequisites
2223

2324
- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/).
2425
- An App Configuration store. [Create a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store).
25-
- [Visual Studio 2019](https://visualstudio.microsoft.com/vs)
26-
- [.NET Framework 4.8](https://dotnet.microsoft.com/download)
26+
27+
### [.NET](#tab/dotnet)
28+
- [.NET SDK](https://dotnet.microsoft.com/download) - also available in the [Azure Cloud Shell](https://shell.azure.com).
29+
30+
### [.NET Framework](#tab/dotnet-framework)
31+
- [.NET Framework 4.7.2 or later](https://dotnet.microsoft.com/download/dotnet-framework)
32+
- [Visual Studio](https://visualstudio.microsoft.com/vs)
33+
34+
---
2735

2836
## Add a feature flag
2937

@@ -32,15 +40,80 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
3240
> [!div class="mx-imgBorder"]
3341
> ![Enable feature flag named Beta](media/add-beta-feature-flag.png)
3442
35-
## Create a .NET Framework console app
43+
## Create a .NET console app
44+
45+
### [.NET](#tab/dotnet)
46+
47+
You use the [.NET command-line interface (CLI)](/dotnet/core/tools/) to create a new .NET console app project. The advantage of using the .NET CLI over Visual Studio is that it's available across the Windows, macOS, and Linux platforms. Alternatively, use the preinstalled tools available in the [Azure Cloud Shell](https://shell.azure.com).
48+
49+
1. Create a new folder for your project.
50+
51+
1. In the new folder, run the following command to create a new .NET console app project:
52+
53+
```dotnetcli
54+
dotnet new console
55+
```
56+
57+
### [.NET Framework](#tab/dotnet-framework)
3658
3759
1. Start Visual Studio, and select **File** > **New** > **Project**.
3860
3961
1. In **Create a new project**, filter on the **Console** project type and click on **Console App (.NET Framework)**. Click **Next**.
4062
4163
1. In **Configure your new project**, enter a project name. Under **Framework**, select **.NET Framework 4.8** or higher. Click **Create**.
4264
43-
## Connect to an App Configuration store
65+
---
66+
67+
## Use feature flag
68+
69+
### [.NET](#tab/dotnet)
70+
71+
1. Add references to the `Microsoft.Extensions.Configuration.AzureAppConfiguration` and `Microsoft.FeatureManagement` NuGet packages by running the following commands.
72+
73+
```dotnetcli
74+
dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
75+
dotnet add package Microsoft.FeatureManagement
76+
```
77+
78+
1. Run the following command to restore packages for your project.
79+
80+
```dotnetcli
81+
dotnet restore
82+
```
83+
84+
1. Open *Program.cs* and add the following statements.
85+
86+
```csharp
87+
using Microsoft.Extensions.Configuration;
88+
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
89+
using Microsoft.FeatureManagement;
90+
```
91+
92+
1. Connect to App Configuration, specifying the `UseFeatureFlags` option so that feature flags are retrieved. Create a `ConfigurationFeatureDefinitionProvider` to provide feature flag definition from the configuration and a `FeatureManager` to evaluate feature flags' state. Then display a message if the `Beta` feature flag is enabled.
93+
94+
```csharp
95+
IConfiguration configuration = new ConfigurationBuilder()
96+
.AddAzureAppConfiguration(options =>
97+
{
98+
options.Connect(Environment.GetEnvironmentVariable("ConnectionString"))
99+
.UseFeatureFlags();
100+
}).Build();
101+
102+
IFeatureDefinitionProvider featureDefinitionProvider = new ConfigurationFeatureDefinitionProvider(configuration);
103+
104+
IFeatureManager featureManager = new FeatureManager(
105+
featureDefinitionProvider,
106+
new FeatureManagementOptions());
107+
108+
if (await featureManager.IsEnabledAsync("Beta"))
109+
{
110+
Console.WriteLine("Welcome to the beta!");
111+
}
112+
113+
Console.WriteLine("Hello World!");
114+
```
115+
116+
### [.NET Framework](#tab/dotnet-framework)
44117
45118
1. Right-click your project, and select **Manage NuGet Packages**. On the **Browse** tab, search and add the following NuGet packages to your project.
46119
@@ -49,7 +122,7 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
49122
Microsoft.FeatureManagement
50123
```
51124
52-
1. Open *Program.cs* and add the following statements:
125+
1. Open *Program.cs* and add the following statements.
53126
54127
```csharp
55128
using Microsoft.Extensions.Configuration;
@@ -82,28 +155,88 @@ Add a feature flag called *Beta* to the App Configuration store and leave **Labe
82155
}
83156
84157
Console.WriteLine("Hello World!");
85-
Console.WriteLine("Press any key to continue ...");
86-
Console.Read();
87158
}
88159
```
89160
161+
---
162+
90163
## Build and run the app locally
91164
92-
1. Set an environment variable named **ConnectionString** to the connection string of your App Configuration store. If you use the Windows command prompt, run the following command:
165+
1. Set an environment variable named **ConnectionString** to the connection string of your App Configuration store. If you use the Windows command prompt, run the following command.
166+
167+
### [Windows command prompt](#tab/windowscommandprompt)
168+
169+
To build and run the app locally using the Windows command prompt, run the following command:
93170
94171
```console
95-
setx ConnectionString "connection-string-of-your-app-configuration-store"
172+
setx ConnectionString "connection-string-of-your-app-configuration-store"
173+
```
174+
175+
Restart the command prompt to allow the change to take effect. Print the value of the environment variable to validate that it's set properly.
176+
177+
### [PowerShell](#tab/powershell)
178+
179+
If you use Windows PowerShell, run the following command.
180+
181+
```azurepowershell
182+
$Env:ConnectionString = "connection-string-of-your-app-configuration-store"
96183
```
97184
98-
If you use Windows PowerShell, run the following command:
185+
### [macOS](#tab/unix)
99186
100-
```powershell
101-
$Env:ConnectionString = "connection-string-of-your-app-configuration-store"
187+
If you use macOS, run the following command.
188+
189+
```console
190+
export ConnectionString='connection-string-of-your-app-configuration-store'
102191
```
103192
104-
1. Restart Visual Studio to allow the change to take effect.
193+
Restart the command prompt to allow the change to take effect. Print the value of the environment variable to validate that it's set properly.
194+
195+
### [Linux](#tab/linux)
196+
197+
If you use Linux, run the following command.
198+
199+
```console
200+
export ConnectionString='connection-string-of-your-app-configuration-store'
201+
```
202+
203+
Restart the command prompt to allow the change to take effect. Print the value of the environment variable to validate that it's set properly.
204+
205+
---
206+
207+
1. Build and run the application.
208+
209+
### [.NET](#tab/dotnet)
210+
211+
- Run the following command to build the console app.
212+
213+
```dotnetcli
214+
dotnet build
215+
```
216+
217+
- After the build successfully completes, run the following command to run the app locally.
218+
219+
```dotnetcli
220+
dotnet run
221+
```
222+
223+
### [.NET Framework](#tab/dotnet-framework)
224+
225+
- Restart Visual Studio to allow the change to take effect.
226+
227+
- Press Ctrl + F5 to build and run the console app.
228+
229+
---
230+
231+
1. You should see the following outputs in the console.
232+
233+
![App with feature flag disabled](./media/quickstarts/dotnet-app-feature-flag-disabled.png)
234+
235+
1. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and select the App Configuration store that you created previously.
236+
237+
1. Select **Feature manager** and locate the **Beta** feature flag. Enable the flag by selecting the checkbox under **Enabled**.
105238
106-
1. Press Ctrl + F5 to build and run the console app.
239+
1. Run the application again. You should see the Beta message in the console.
107240
108241
![App with feature flag enabled](./media/quickstarts/dotnet-app-feature-flag.png)
109242

0 commit comments

Comments
 (0)