|
1 | | -using Microsoft.Extensions.Configuration; |
2 | | -using Microsoft.Extensions.DependencyInjection; |
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | +// |
| 4 | +using Microsoft.Extensions.Configuration; |
3 | 5 | using Microsoft.FeatureManagement; |
4 | 6 | using Microsoft.FeatureManagement.FeatureFilters; |
5 | 7 | using TargetingConsoleApp.Identity; |
|
10 | 12 | .AddJsonFile("appsettings.json") |
11 | 13 | .Build(); |
12 | 14 |
|
13 | | -// |
14 | | -// Setup application services + feature management |
15 | | -IServiceCollection services = new ServiceCollection(); |
16 | | - |
17 | | -services.AddSingleton(configuration) |
18 | | - .AddFeatureManagement(); |
| 15 | +var featureManager = new FeatureManager(new ConfigurationFeatureDefinitionProvider(configuration)) |
| 16 | +{ |
| 17 | + FeatureFilters = new List<IFeatureFilterMetadata> { new ContextualTargetingFilter() } |
| 18 | +}; |
19 | 19 |
|
20 | 20 | var userRepository = new InMemoryUserRepository(); |
21 | 21 |
|
22 | 22 | // |
23 | | -// Get the feature manager from application services |
24 | | -using (ServiceProvider serviceProvider = services.BuildServiceProvider()) |
| 23 | +// We'll simulate a task to run on behalf of each known user |
| 24 | +// To do this we enumerate all the users in our user repository |
| 25 | +IEnumerable<string> userIds = InMemoryUserRepository.Users.Select(u => u.Id); |
| 26 | + |
| 27 | +// |
| 28 | +// Mimic work items in a task-driven console application |
| 29 | +foreach (string userId in userIds) |
25 | 30 | { |
26 | | - IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>(); |
| 31 | + const string FeatureName = "Beta"; |
27 | 32 |
|
28 | 33 | // |
29 | | - // We'll simulate a task to run on behalf of each known user |
30 | | - // To do this we enumerate all the users in our user repository |
31 | | - IEnumerable<string> userIds = InMemoryUserRepository.Users.Select(u => u.Id); |
| 34 | + // Get user |
| 35 | + User user = await userRepository.GetUser(userId); |
32 | 36 |
|
33 | 37 | // |
34 | | - // Mimic work items in a task-driven console application |
35 | | - foreach (string userId in userIds) |
| 38 | + // Check if feature enabled |
| 39 | + var targetingContext = new TargetingContext |
36 | 40 | { |
37 | | - const string FeatureName = "Beta"; |
38 | | - |
39 | | - // |
40 | | - // Get user |
41 | | - User user = await userRepository.GetUser(userId); |
42 | | - |
43 | | - // |
44 | | - // Check if feature enabled |
45 | | - var targetingContext = new TargetingContext |
46 | | - { |
47 | | - UserId = user.Id, |
48 | | - Groups = user.Groups |
49 | | - }; |
50 | | - |
51 | | - bool enabled = await featureManager.IsEnabledAsync(FeatureName, targetingContext); |
52 | | - |
53 | | - // |
54 | | - // Output results |
55 | | - Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the user '{userId}'."); |
56 | | - } |
| 41 | + UserId = user.Id, |
| 42 | + Groups = user.Groups |
| 43 | + }; |
| 44 | + |
| 45 | + bool enabled = await featureManager.IsEnabledAsync(FeatureName, targetingContext); |
| 46 | + |
| 47 | + // |
| 48 | + // Output results |
| 49 | + Console.WriteLine($"The {FeatureName} feature is {(enabled ? "enabled" : "disabled")} for the user '{userId}'."); |
57 | 50 | } |
0 commit comments