diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs index 2f350a9..3c7ceff 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Extensions/ApplicationInsightsExtensions.cs @@ -190,7 +190,7 @@ namespace Microsoft.Extensions.DependencyInjection /// public static IServiceCollection AddApplicationInsightsTelemetryProcessor(this IServiceCollection services) where T : ITelemetryProcessor { - return services.AddSingleton(new TelemetryProcessorFactory()); + return services.AddSingleton(serviceProvider => new TelemetryProcessorFactory(serviceProvider)); } /// diff --git a/src/Microsoft.ApplicationInsights.AspNetCore/Implementation/TelemetryProcessorFactory.cs b/src/Microsoft.ApplicationInsights.AspNetCore/Implementation/TelemetryProcessorFactory.cs index c33332d..97c7194 100644 --- a/src/Microsoft.ApplicationInsights.AspNetCore/Implementation/TelemetryProcessorFactory.cs +++ b/src/Microsoft.ApplicationInsights.AspNetCore/Implementation/TelemetryProcessorFactory.cs @@ -2,6 +2,7 @@ { using System; using Microsoft.ApplicationInsights.Extensibility; + using Microsoft.Extensions.DependencyInjection; /// /// A generic factory for telemetry processors of a given type. @@ -9,6 +10,17 @@ /// The type of telemetry processor created by this factory. internal class TelemetryProcessorFactory : ITelemetryProcessorFactory where T : ITelemetryProcessor { + private readonly IServiceProvider serviceProvider; + + /// + /// Constructs an instance of the factory. + /// + /// The service provider. + public TelemetryProcessorFactory(IServiceProvider serviceProvider) + { + this.serviceProvider = serviceProvider; + } + /// /// Creates an instance of the telemetry processor, passing the /// next in the call chain to @@ -16,7 +28,7 @@ /// public ITelemetryProcessor Create(ITelemetryProcessor next) { - return (ITelemetryProcessor)Activator.CreateInstance(typeof(T), args: next); + return ActivatorUtilities.CreateInstance(serviceProvider, next); } } }