@@ -16,6 +16,29 @@ namespace Microsoft.FeatureManagement
1616 /// </summary>
1717 public static class FeatureManagementBuilderExtensions
1818 {
19+ /// <summary>
20+ /// Adds an <see cref="ITargetingContextAccessor"/> to be used for targeting and registers the targeting filter to the feature management system.
21+ /// </summary>
22+ /// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
23+ /// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
24+ public static IFeatureManagementBuilder WithTargeting < T > ( this IFeatureManagementBuilder builder ) where T : ITargetingContextAccessor
25+ {
26+ //
27+ // Register the targeting context accessor with the same lifetime as the feature manager
28+ if ( builder . Services . Any ( descriptor => descriptor . ServiceType == typeof ( IFeatureManager ) && descriptor . Lifetime == ServiceLifetime . Scoped ) )
29+ {
30+ builder . Services . TryAddScoped ( typeof ( ITargetingContextAccessor ) , typeof ( T ) ) ;
31+ }
32+ else
33+ {
34+ builder . Services . TryAddSingleton ( typeof ( ITargetingContextAccessor ) , typeof ( T ) ) ;
35+ }
36+
37+ builder . AddFeatureFilter < TargetingFilter > ( ) ;
38+
39+ return builder ;
40+ }
41+
1942 /// <summary>
2043 /// Adds a telemetry publisher to the feature management system.
2144 /// </summary>
@@ -42,28 +65,5 @@ private static IFeatureManagementBuilder AddTelemetryPublisher(this IFeatureMana
4265
4366 return builder ;
4467 }
45-
46- /// <summary>
47- /// Adds an <see cref="ITargetingContextAccessor"/> to be used for targeting and registers the targeting filter to the feature management system.
48- /// </summary>
49- /// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
50- /// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
51- public static IFeatureManagementBuilder WithTargeting < T > ( this IFeatureManagementBuilder builder ) where T : ITargetingContextAccessor
52- {
53- //
54- // Register the targeting context accessor with the same lifetime as the feature manager
55- if ( builder . Services . Any ( descriptor => descriptor . ServiceType == typeof ( IFeatureManager ) && descriptor . Lifetime == ServiceLifetime . Scoped ) )
56- {
57- builder . Services . TryAddScoped ( typeof ( ITargetingContextAccessor ) , typeof ( T ) ) ;
58- }
59- else
60- {
61- builder . Services . TryAddSingleton ( typeof ( ITargetingContextAccessor ) , typeof ( T ) ) ;
62- }
63-
64- builder . AddFeatureFilter < TargetingFilter > ( ) ;
65-
66- return builder ;
67- }
6868 }
6969}
0 commit comments