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(); diff --git a/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs b/src/Microsoft.FeatureManagement.AspNetCore/AspNetCoreFeatureManagementBuilderExtensions.cs index 9091a4b0..43aa6b46 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)) diff --git a/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs b/src/Microsoft.FeatureManagement.Telemetry.ApplicationInsights/FeatureManagementBuilderExtensions.cs index 002d2bfc..503f4776 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; @@ -13,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) { @@ -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)))