diff --git a/CHANGELOG.md b/CHANGELOG.md index 049c0f363..829cc6749 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## VNext - [Populate required field Message with "n/a" if it is empty](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1066) +- [Fix ITelemetryModule singleton registration to support presence of keyed services](https://github.com/microsoft/ApplicationInsights-dotnet/pull/2908) ## Version 2.22.0 - no changes since beta. diff --git a/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs b/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs index efbce45c4..e726712e2 100644 --- a/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs +++ b/NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs @@ -312,7 +312,7 @@ private static void AddCommonTelemetryModules(IServiceCollection services) { // Previously users were encouraged to manually add the DiagnosticsTelemetryModule. - services.TryAddSingleton(); + services.TryAddEnumerable(ServiceDescriptor.Singleton()); // These modules add properties to Heartbeat and expect the DiagnosticsTelemetryModule to be configured in DI. services.AddSingleton(); diff --git a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs index 69956615d..01e4c027c 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs +++ b/NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs @@ -37,14 +37,22 @@ namespace Microsoft.Extensions.DependencyInjection.Test public class AddApplicationInsightsTelemetryTests : BaseTestClass { - [Fact] - public static void TelemetryModulesResolvableWhenKeyedServiceRegistered() + [Theory] + [InlineData(true)] + [InlineData(false)] + public static void TelemetryModulesResolvableWhenKeyedServiceRegistered(bool manuallyRegisterDiagnosticsTelemetryModule) { // Note: This test verifies a regression doesn't get introduced for: - // https://github.com/dotnet/extensions/issues/5222 + // https://github.com/microsoft/ApplicationInsights-dotnet/issues/2879 var services = new ServiceCollection(); + services.AddSingleton(); + if (manuallyRegisterDiagnosticsTelemetryModule) + { + services.AddSingleton(); + } + services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationType: typeof(TestService)); services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationInstance: new TestService()); services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationFactory: (sp, key) => new TestService()); @@ -55,7 +63,9 @@ namespace Microsoft.Extensions.DependencyInjection.Test var telemetryModules = sp.GetServices(); - Assert.Equal(7, telemetryModules.Count()); + Assert.Equal(8, telemetryModules.Count()); + + Assert.Single(telemetryModules.Where(m => m.GetType() == typeof(DiagnosticsTelemetryModule))); } [Theory] diff --git a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/ExtensionsTest.cs b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/ExtensionsTest.cs index d4503e9f1..f0d5661c8 100644 --- a/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/ExtensionsTest.cs +++ b/NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/ExtensionsTest.cs @@ -123,14 +123,22 @@ namespace Microsoft.ApplicationInsights.WorkerService.Tests return services; } - [Fact] - public static void TelemetryModulesResolvableWhenKeyedServiceRegistered() + [Theory] + [InlineData(true)] + [InlineData(false)] + public static void TelemetryModulesResolvableWhenKeyedServiceRegistered(bool manuallyRegisterDiagnosticsTelemetryModule) { // Note: This test verifies a regression doesn't get introduced for: - // https://github.com/dotnet/extensions/issues/5222 + // https://github.com/microsoft/ApplicationInsights-dotnet/issues/2879 var services = new ServiceCollection(); + services.AddSingleton(); + if (manuallyRegisterDiagnosticsTelemetryModule) + { + services.AddSingleton(); + } + services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationType: typeof(TestService)); services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationInstance: new TestService()); services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationFactory: (sp, key) => new TestService()); @@ -141,7 +149,9 @@ namespace Microsoft.ApplicationInsights.WorkerService.Tests var telemetryModules = sp.GetServices(); - Assert.Equal(7, telemetryModules.Count()); + Assert.Equal(8, telemetryModules.Count()); + + Assert.Single(telemetryModules.Where(m => m.GetType() == typeof(DiagnosticsTelemetryModule))); } [Theory] @@ -921,6 +931,13 @@ namespace Microsoft.ApplicationInsights.WorkerService.Tests { } + private sealed class TestTelemetryModule : ITelemetryModule + { + public void Initialize(TelemetryConfiguration configuration) + { + } + } + private interface ITestService { }