Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ namespace Microsoft.FeatureManagement
/// </summary>
public static class FeatureManagementBuilderExtensions
{
/// <summary>
/// Adds an <see cref="ITargetingContextAccessor"/> to be used for targeting and registers the targeting filter to the feature management system.
/// </summary>
/// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
public static IFeatureManagementBuilder WithTargeting<T>(this IFeatureManagementBuilder builder) where T : ITargetingContextAccessor
{
//
// Register the targeting context accessor with the same lifetime as the feature manager
if (builder.Services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
{
builder.Services.TryAddScoped(typeof(ITargetingContextAccessor), typeof(T));
}
else
{
builder.Services.TryAddSingleton(typeof(ITargetingContextAccessor), typeof(T));
}

builder.AddFeatureFilter<TargetingFilter>();

return builder;
}

/// <summary>
/// Adds a telemetry publisher to the feature management system.
/// </summary>
Expand All @@ -42,28 +65,5 @@ private static IFeatureManagementBuilder AddTelemetryPublisher(this IFeatureMana

return builder;
}

/// <summary>
/// Adds an <see cref="ITargetingContextAccessor"/> to be used for targeting and registers the targeting filter to the feature management system.
/// </summary>
/// <param name="builder">The <see cref="IFeatureManagementBuilder"/> used to customize feature management functionality.</param>
/// <returns>A <see cref="IFeatureManagementBuilder"/> that can be used to customize feature management functionality.</returns>
public static IFeatureManagementBuilder WithTargeting<T>(this IFeatureManagementBuilder builder) where T : ITargetingContextAccessor
{
//
// Register the targeting context accessor with the same lifetime as the feature manager
if (builder.Services.Any(descriptor => descriptor.ServiceType == typeof(IFeatureManager) && descriptor.Lifetime == ServiceLifetime.Scoped))
{
builder.Services.TryAddScoped(typeof(ITargetingContextAccessor), typeof(T));
}
else
{
builder.Services.TryAddSingleton(typeof(ITargetingContextAccessor), typeof(T));
}

builder.AddFeatureFilter<TargetingFilter>();

return builder;
}
}
}