Skip to content

Commit a988f74

Browse files
authored
Merge pull request #101169 from jpconnock/feature-flags
ASP.Net Core QS with Feature flags
2 parents 812b678 + 404f593 commit a988f74

File tree

2 files changed

+97
-38
lines changed

2 files changed

+97
-38
lines changed
29.9 KB
Loading

articles/azure-app-configuration/quickstart-feature-flag-aspnet-core.md

Lines changed: 97 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
---
2-
title: Quickstart for adding feature flags to ASP.NET Core | Microsoft Docs
3-
description: A quickstart for adding feature flags to ASP.NET Core apps and managing them in Azure App Configuration
4-
services: azure-app-configuration
5-
documentationcenter: ''
6-
author: yegu-ms
7-
manager: maiye
8-
editor: ''
9-
10-
ms.assetid:
2+
title: Quickstart for adding feature flags to ASP.NET Core
3+
description: Add feature flags to ASP.NET Core apps and manage them using Azure App Configuration
4+
author: jpconnock
5+
116
ms.service: azure-app-configuration
12-
ms.devlang: csharp
137
ms.topic: quickstart
14-
ms.tgt_pltfrm: ASP.NET Core
15-
ms.workload: tbd
16-
ms.date: 04/19/2019
17-
ms.author: yegu
8+
ms.date: 01/14/2020
9+
ms.author: jeconnoc
1810

1911
#Customer intent: As an ASP.NET Core developer, I want to use feature flags to control feature availability quickly and confidently.
2012
---
2113

2214
# Quickstart: Add feature flags to an ASP.NET Core app
2315

24-
In this quickstart, you incorporate Azure App Configuration into an ASP.NET Core web 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.
16+
In this quickstart, you create an end-to-end implementation of feature management in an ASP.NET Core application using Azure App Configuration. You will use the App Configuration service to centrally store all your feature flags and control their states.
2517

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

@@ -34,15 +26,16 @@ The .NET Core Feature Management libraries extend the framework with comprehensi
3426

3527
[!INCLUDE [azure-app-configuration-create](../../includes/azure-app-configuration-create.md)]
3628

37-
6. Select **Feature Manager** > **+Add** to add the following feature flags:
29+
6. Select **Feature Manager** > **+Add** to add a feature flag called `Beta`.
30+
31+
> [!div class="mx-imgBorder"]
32+
> ![Enable feature flag named Beta](media/add-beta-feature-flag.png)
3833
39-
| Key | State |
40-
|---|---|
41-
| Beta | Off |
34+
Leave `label` undefined for now.
4235

4336
## Create an ASP.NET Core web app
4437

45-
You use the [.NET Core command-line interface (CLI)](https://docs.microsoft.com/dotnet/core/tools/) to create a new ASP.NET Core MVC web app project. The advantage of using the .NET Core CLI instead of Visual Studio is that the .NET Core CLI is available across the Windows, macOS, and Linux platforms.
38+
Use the [.NET Core command-line interface (CLI)](https://docs.microsoft.com/dotnet/core/tools/) to create a new ASP.NET Core MVC web app project. The advantage of using the .NET Core CLI instead of Visual Studio is that the .NET Core CLI is available across the Windows, macOS, and Linux platforms.
4639

4740
1. Create a new folder for your project. For this quickstart, name it *TestFeatureFlags*.
4841

@@ -56,9 +49,13 @@ You use the [.NET Core command-line interface (CLI)](https://docs.microsoft.com/
5649

5750
Add the [Secret Manager tool](https://docs.microsoft.com/aspnet/core/security/app-secrets) to your project. The Secret Manager tool stores sensitive data for development work outside your project tree. This approach helps prevent the accidental sharing of app secrets within source code.
5851

52+
> [!IMPORTANT]
53+
> Significant differences exist between .NET Core 2.x and 3.x. Select the correct syntax based on your environment.
54+
5955
1. Open the *.csproj* file.
6056
1. Add a `UserSecretsId` element as shown in the following example, and replace its value with your own, which typically is a GUID:
6157

58+
#### [.NET Core 2.x](#tab/core2x)
6259
```xml
6360
<Project Sdk="Microsoft.NET.Sdk.Web">
6461

@@ -74,15 +71,24 @@ Add the [Secret Manager tool](https://docs.microsoft.com/aspnet/core/security/ap
7471

7572
</Project>
7673
```
77-
78-
1. Save the file.
74+
#### [.NET Core 3.x](#tab/core3x)
75+
```xml
76+
<Project Sdk="Microsoft.NET.Sdk.Web">
77+
78+
<PropertyGroup>
79+
<TargetFramework>netcoreapp3.1</TargetFramework>
80+
<UserSecretsId>79a3edd0-2092-40a2-a04d-dcb46d5ca9ed</UserSecretsId>
81+
</PropertyGroup>
82+
</Project>
83+
```
84+
---
7985

8086
## Connect to an App Configuration store
8187

8288
1. Add reference to the `Microsoft.Azure.AppConfiguration.AspNetCore` and the `Microsoft.FeatureManagement.AspNetCore` NuGet packages by running the following commands:
8389

8490
```
85-
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore --version 3.0.0-preview-010560002-1165
91+
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore --version 3.0.0-preview-011100002-1192
8692
dotnet add package Microsoft.FeatureManagement.AspNetCore --version 2.0.0-preview-010610001-1263
8793
```
8894

@@ -111,8 +117,8 @@ Add the [Secret Manager tool](https://docs.microsoft.com/aspnet/core/security/ap
111117
> [!IMPORTANT]
112118
> `CreateHostBuilder` replaces `CreateWebHostBuilder` in .NET Core 3.0. Select the correct syntax based on your environment.
113119

114-
### Update `CreateWebHostBuilder` for .NET Core 2.x
115-
120+
#### [.NET Core 2.x](#tab/core2x)
121+
116122
```csharp
117123
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
118124
WebHost.CreateDefaultBuilder(args)
@@ -127,8 +133,8 @@ Add the [Secret Manager tool](https://docs.microsoft.com/aspnet/core/security/ap
127133
.UseStartup<Startup>();
128134
```
129135

130-
### Update `CreateHostBuilder` for .NET Core 3.x
131-
136+
#### [.NET Core 3.x](#tab/core3x)
137+
132138
```csharp
133139
public static IHostBuilder CreateHostBuilder(string[] args) =>
134140
Host.CreateDefaultBuilder(args)
@@ -143,7 +149,7 @@ Add the [Secret Manager tool](https://docs.microsoft.com/aspnet/core/security/ap
143149
})
144150
.UseStartup<Startup>());
145151
```
146-
152+
---
147153

148154
1. Open *Startup.cs*, and add references to the .NET Core feature manager:
149155

@@ -153,22 +159,75 @@ Add the [Secret Manager tool](https://docs.microsoft.com/aspnet/core/security/ap
153159

154160
1. Update the `ConfigureServices` method to add feature flag support by calling the `services.AddFeatureManagement()` method. Optionally, you can include any filter to be used with feature flags by calling `services.AddFeatureFilter<FilterType>()`:
155161

162+
#### [.NET Core 2.x](#tab/core2x)
156163
```csharp
157164
public void ConfigureServices(IServiceCollection services)
158165
{
166+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
167+
services.AddFeatureManagement();
168+
}
169+
```
170+
#### [.NET Core 3.x](#tab/core3x)
171+
```csharp
172+
public void ConfigureServices(IServiceCollection services)
173+
{
174+
services.AddControllersWithViews();
159175
services.AddFeatureManagement();
160176
}
161177
```
178+
---
162179

163180
1. Update the `Configure` method to add a middleware to allow the feature flag values to be refreshed at a recurring interval while the ASP.NET Core web app continues to receive requests.
164-
181+
182+
#### [.NET Core 2.x](#tab/core2x)
183+
```csharp
184+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
185+
{
186+
if (env.IsDevelopment())
187+
{
188+
app.UseDeveloperExceptionPage();
189+
}
190+
else
191+
{
192+
app.UseExceptionHandler("/Home/Error");
193+
}
194+
195+
app.UseStaticFiles();
196+
app.UseCookiePolicy();
197+
app.UseAzureAppConfiguration();
198+
app.UseMvc(routes =>
199+
{
200+
routes.MapRoute(
201+
name: "default",
202+
template: "{controller=Home}/{action=Index}/{id?}");
203+
});
204+
}
205+
```
206+
#### [.NET Core 3.x](#tab/core3x)
165207
```csharp
166208
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
167209
{
168-
app.UseAzureAppConfiguration();
169-
app.UseMvc();
210+
if (env.IsDevelopment())
211+
{
212+
app.UseDeveloperExceptionPage();
213+
}
214+
else
215+
{
216+
app.UseExceptionHandler("/Home/Error");
217+
}
218+
app.UseStaticFiles();
219+
app.UseRouting();
220+
app.UseAuthorization();
221+
app.UseEndpoints(endpoints =>
222+
{
223+
endpoints.MapControllerRoute(
224+
name: "default",
225+
pattern: "{controller=Home}/{action=Index}/{id?}");
226+
});
227+
app.UseAzureAppConfiguration();
170228
}
171229
```
230+
---
172231

173232
1. Add a *MyFeatureFlags.cs* file:
174233

@@ -270,19 +329,19 @@ Add the [Secret Manager tool](https://docs.microsoft.com/aspnet/core/security/ap
270329
dotnet run
271330
```
272331

273-
1. Open a browser window, and go to `https://localhost:5001`, which is the default URL for the web app hosted locally.
332+
1. Open a browser window, and go to `https://localhost:5000`, which is the default URL for the web app hosted locally.
333+
If you're working in the Azure Cloud Shell, select the *Web Preview* button followed by *Configure*. When prompted, select port 5000.
274334

335+
![Locate the Web Preview button](./media/quickstarts/cloud-shell-web-preview.png)
336+
337+
Your browser should display a page similar to the image below.
275338
![Quickstart app launch local](./media/quickstarts/aspnet-core-feature-flag-local-before.png)
276339

277340
1. 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.
278341

279-
1. Select **Feature Manager**, and change the state of the **Beta** key to **On**:
280-
281-
| Key | State |
282-
|---|---|
283-
| Beta | On |
342+
1. Select **Feature Manager**, and change the state of the **Beta** key to **On**.
284343

285-
1. Restart your application by switching back to your command prompt and pressing `Ctrl-C` to cancel the running `dotnet` process, then rerunning `dotnet run`.
344+
1. Return to the command prompt and cancel the running `dotnet` process by pressing `Ctrl-C`. Restart your application using `dotnet run`.
286345

287346
1. Refresh the browser page to see the new configuration settings.
288347

0 commit comments

Comments
 (0)