From f689972b25e7b7d97e8c54f054df1aad85847fcf Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Tue, 20 Aug 2024 16:33:52 -0700 Subject: [PATCH 1/3] Adds and adjusts builder extension methods --- ...tCoreFeatureManagementBuilderExtensions.cs | 20 +++++++++++++++++++ .../FeatureManagementBuilderExtensions.cs | 4 ++++ 2 files changed, 24 insertions(+) diff --git a/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs b/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs index 9091a4b0..c0b5670c 100644 --- a/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs +++ b/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs @@ -1,6 +1,8 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. // +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -55,6 +57,12 @@ public static IFeatureManagementBuilder UseDisabledFeaturesHandler(this IFeature /// A that can be used to customize feature management functionality. public static IFeatureManagementBuilder WithTargeting(this IFeatureManagementBuilder builder) { + // Add HttpContextAccessor if it doesn't already exist + if (!builder.Services.Any(service => service.ServiceType == typeof(IHttpContextAccessor))) + { + builder.Services.AddHttpContextAccessor(); + } + // // 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)) @@ -70,5 +78,17 @@ public static IFeatureManagementBuilder WithTargeting(this IFeatureManagementBui return builder; } + + /// + /// Adds middleware to the application's request pipeline that adds targeting information to the HTTP context. + /// + /// The used to customize feature management functionality. + /// A that can be used to customize feature management functionality. + public static IApplicationBuilder UseFeatureManagement(this IApplicationBuilder builder) + { + builder.UseMiddleware(); + + return builder; + } } } diff --git a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs index 002d2bfc..f63e74b5 100644 --- a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs +++ b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs @@ -1,7 +1,9 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. // +using Microsoft.ApplicationInsights.Extensibility; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.FeatureManagement.Telemetry.ApplicationInsights; @@ -29,6 +31,8 @@ public static IFeatureManagementBuilder AddApplicationInsightsTelemetryPublisher throw new ArgumentException($"The provided builder's services must not be null.", nameof(builder)); } + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); if (!builder.Services.Any((ServiceDescriptor d) => d.ServiceType == typeof(IHostedService) && d.ImplementationType == typeof(ApplicationInsightsHostedService))) From 7ecc5a6f4a0fed7d0611c0d8238fa053414196c7 Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Fri, 23 Aug 2024 09:54:02 -0700 Subject: [PATCH 2/3] Removed UseFeatureManagement and adjusted name of AddAppInsightsTelemetryPublisher --- .../AspNetCoreFeatureManagementBuilderExtensions.cs | 12 ------------ .../FeatureManagementBuilderExtensions.cs | 4 ++-- 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs b/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs index c0b5670c..43aa6b46 100644 --- a/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs +++ b/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs @@ -78,17 +78,5 @@ public static IFeatureManagementBuilder WithTargeting(this IFeatureManagementBui return builder; } - - /// - /// Adds middleware to the application's request pipeline that adds targeting information to the HTTP context. - /// - /// The used to customize feature management functionality. - /// A that can be used to customize feature management functionality. - public static IApplicationBuilder UseFeatureManagement(this IApplicationBuilder builder) - { - builder.UseMiddleware(); - - return builder; - } } } diff --git a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs index f63e74b5..503f4776 100644 --- a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs +++ b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs @@ -15,11 +15,11 @@ namespace Microsoft.FeatureManagement public static class FeatureManagementBuilderExtensions { /// - /// Adds the using to the feature management builder. + /// Adds the and the using to the feature management builder. /// /// The feature management builder. /// The feature management builder. - public static IFeatureManagementBuilder AddApplicationInsightsTelemetryPublisher(this IFeatureManagementBuilder builder) + public static IFeatureManagementBuilder AddApplicationInsightsTelemetry(this IFeatureManagementBuilder builder) { if (builder == null) { From e6b249756cb67a83144900bbbe22a93e92399dc9 Mon Sep 17 00:00:00 2001 From: Ross Grambo Date: Fri, 23 Aug 2024 10:12:35 -0700 Subject: [PATCH 3/3] Adjusts examples --- examples/EvaluationDataToApplicationInsights/Program.cs | 2 +- examples/VariantServiceDemo/Program.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/EvaluationDataToApplicationInsights/Program.cs b/examples/EvaluationDataToApplicationInsights/Program.cs index bf83d5d6..dbc856fc 100644 --- a/examples/EvaluationDataToApplicationInsights/Program.cs +++ b/examples/EvaluationDataToApplicationInsights/Program.cs @@ -30,7 +30,7 @@ // Wire up evaluation event emission builder.Services.AddFeatureManagement() .WithTargeting() - .AddApplicationInsightsTelemetryPublisher(); + .AddApplicationInsightsTelemetry(); // // Default code from .NET template below diff --git a/examples/VariantServiceDemo/Program.cs b/examples/VariantServiceDemo/Program.cs index 843cd1ce..8ac9a031 100644 --- a/examples/VariantServiceDemo/Program.cs +++ b/examples/VariantServiceDemo/Program.cs @@ -37,7 +37,7 @@ builder.Services.AddFeatureManagement() .WithTargeting() .WithVariantService("Calculator") - .AddApplicationInsightsTelemetryPublisher(); + .AddApplicationInsightsTelemetry(); var app = builder.Build();