Skip to content

Commit e55efdd

Browse files
use WithTargeting method
1 parent f000f76 commit e55efdd

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

articles/azure-app-configuration/howto-targetingfilter-aspnet-core.md

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ ms.date: 02/16/2024
1212

1313
# Enable staged rollout of features for targeted audiences
1414

15-
Feature flags allow you to dynamically activate or deactivate functionality in your application. Feature filters determine the state of a feature flag each time it's evaluated. The `Microsoft.FeatureManagement` library includes `TargetingFilter`, which enables a feature flag for a specified list of users and groups, or for a specified percentage of users. `TargetingFilter` is "sticky." This means that once an individual user receives a feature, they'll continue to see that feature on all future requests. You can use `TargetingFilter` to enable a feature for a specific account during a demo, to progressively roll out new features to users in different groups or "rings," and much more.
15+
Feature flags allow you to dynamically activate or deactivate functionality in your application. Feature filters determine the state of a feature flag each time it's evaluated. The `Microsoft.FeatureManagement` library includes `TargetingFilter`, which enables a feature flag for a specified list of users and groups, or for a specified percentage of users. `TargetingFilter` is "sticky." This means that once an individual user receives a feature, they'll continue to see that feature on all future requests. You can use `TargetingFilter` to enable a feature for a specific account during a demo, to progressively roll out new features to users in different groups or "rings," and much more. For more information, see [Targeting](https://github.com/microsoft/FeatureManagement-Dotnet#targeting).
1616

1717
In this article, you learn how to roll out a new feature in an ASP.NET Core web application to specified users and groups, using `TargetingFilter` with Azure App Configuration.
1818

1919
## Prerequisites
2020

2121
- Finish the [Quickstart: Add feature flags to an ASP.NET Core app](./quickstart-feature-flag-aspnet-core.md).
22-
- Update the `Microsoft.FeatureManagement.AspNetCore` package to version **2.6.0** or later.
22+
- Update the [`Microsoft.FeatureManagement.AspNetCore`](https://www.nuget.org/packages/Microsoft.FeatureManagement.AspNetCore/) package to version **3.0.0** or later.
2323

2424
## Create a web application with feature flags and authentication
2525

@@ -92,48 +92,37 @@ At this point, you can use the feature flag to enable or disable the `Beta` feat
9292
}
9393
```
9494

95-
1. In *Startup.cs*, add a reference to the *Microsoft.FeatureManagement.FeatureFilters* namespace.
95+
1. In *Program.cs*, add a reference to the *Microsoft.FeatureManagement.FeatureFilters* namespace.
9696

9797
```csharp
9898
using Microsoft.FeatureManagement.FeatureFilters;
9999
```
100100

101-
1. Update the *ConfigureServices* method to register `TargetingFilter`, following the call to `AddFeatureManagement()`.
101+
1. Register `TargetingFilter` and `TestTargetingContextAccessor` created in the earlier step to the service collection. The `TargetingFilter` will use the `TestTargetingContextAccessor` to determine the targeting context every time that the feature flag is evaluated.
102102

103-
```csharp
104-
services.AddFeatureManagement()
105-
.AddFeatureFilter<TargetingFilter>();
106-
```
107-
108-
> [!NOTE]
109-
> For Blazor applications, see [instructions](./faq.yml#how-to-enable-feature-management-in-blazor-applications-or-as-scoped-services-in--net-applications) for enabling feature management as scoped services.
103+
### [Microsoft.FeatureManagement.AspNetCore 3.0.0+](#tab/fm3x)
110104

111-
1. Update the *ConfigureServices* method to add the `TestTargetingContextAccessor` created in the earlier step to the service collection. The *TargetingFilter* uses it to determine the targeting context every time that the feature flag is evaluated.
105+
Since `Microsoft.FeatureManagement` 3.0.0, you can use `WithTargeting` method to register `TargetingFilter` and `ITargetingContextAccessor` at the same time.
112106

113107
```csharp
114-
services.AddSingleton<ITargetingContextAccessor, TestTargetingContextAccessor>();
108+
services.AddFeatureManagement()
109+
.WithTargeting<TestTargetingContextAccessor>();
115110
```
116111

117-
The entire *ConfigureServices* method looks like this.
112+
### [Microsoft.FeatureManagement.AspNetCore 2.6.x](#tab/fm2x)
118113

119114
```csharp
120-
public void ConfigureServices(IServiceCollection services)
121-
{
122-
services.AddDbContext<ApplicationDbContext>(options =>
123-
options.UseSqlite(
124-
Configuration.GetConnectionString("DefaultConnection")));
125-
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
126-
.AddEntityFrameworkStores<ApplicationDbContext>();
127-
services.AddControllersWithViews();
128-
services.AddRazorPages();
129-
130-
// Add feature management, targeting filter, and ITargetingContextAccessor to service collection
131-
services.AddFeatureManagement()
132-
.AddFeatureFilter<TargetingFilter>();
133-
services.AddSingleton<ITargetingContextAccessor, TestTargetingContextAccessor>();
134-
}
115+
services.AddFeatureManagement()
116+
.AddFeatureFilter<TargetingFilter>();
117+
118+
services.AddSingleton<ITargetingContextAccessor, TestTargetingContextAccessor>();
135119
```
136120

121+
---
122+
123+
> [!NOTE]
124+
> For Blazor applications, see [instructions](./faq.yml#how-to-enable-feature-management-in-blazor-applications-or-as-scoped-services-in--net-applications) for enabling feature management as scoped services.
125+
137126
## Update the feature flag to use TargetingFilter
138127

139128
1. In the Azure portal, go to your App Configuration store and select **Feature manager**.

0 commit comments

Comments
 (0)