Skip to content

Commit 42cd7e3

Browse files
committed
Tweaks and CHANGELOG update.
1 parent d5f7d8b commit 42cd7e3

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## VNext
44
- [Populate required field Message with "n/a" if it is empty](https://github.com/microsoft/ApplicationInsights-dotnet/issues/1066)
5+
- [Fix ITelemetryModule singleton registration to support presence of keyed services](https://github.com/microsoft/ApplicationInsights-dotnet/pull/2908)
56

67
## Version 2.22.0
78
- no changes since beta.

NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ private static void AddCommonInitializers(IServiceCollection services)
312312
private static void AddCommonTelemetryModules(IServiceCollection services)
313313
{
314314
// Previously users were encouraged to manually add the DiagnosticsTelemetryModule.
315-
services.TryAddSingleton<ITelemetryModule, DiagnosticsTelemetryModule>();
315+
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITelemetryModule, DiagnosticsTelemetryModule>());
316316

317317
// These modules add properties to Heartbeat and expect the DiagnosticsTelemetryModule to be configured in DI.
318318
services.AddSingleton<ITelemetryModule, AppServicesHeartbeatTelemetryModule>();

NETCORE/test/Microsoft.ApplicationInsights.AspNetCore.Tests/Extensions/ApplicationInsightsExtensionsTests/AddApplicationInsightsTelemetryTests.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,22 @@ namespace Microsoft.Extensions.DependencyInjection.Test
3737

3838
public class AddApplicationInsightsTelemetryTests : BaseTestClass
3939
{
40-
[Fact]
41-
public static void TelemetryModulesResolvableWhenKeyedServiceRegistered()
40+
[Theory]
41+
[InlineData(true)]
42+
[InlineData(false)]
43+
public static void TelemetryModulesResolvableWhenKeyedServiceRegistered(bool manuallyRegisterDiagnosticsTelemetryModule)
4244
{
4345
// Note: This test verifies a regression doesn't get introduced for:
44-
// https://github.com/dotnet/extensions/issues/5222
46+
// https://github.com/microsoft/ApplicationInsights-dotnet/issues/2879
4547

4648
var services = new ServiceCollection();
4749

50+
services.AddSingleton<ITelemetryModule, TestTelemetryModule>();
51+
if (manuallyRegisterDiagnosticsTelemetryModule)
52+
{
53+
services.AddSingleton<ITelemetryModule, DiagnosticsTelemetryModule>();
54+
}
55+
4856
services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationType: typeof(TestService));
4957
services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationInstance: new TestService());
5058
services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationFactory: (sp, key) => new TestService());
@@ -55,7 +63,9 @@ public static void TelemetryModulesResolvableWhenKeyedServiceRegistered()
5563

5664
var telemetryModules = sp.GetServices<ITelemetryModule>();
5765

58-
Assert.Equal(7, telemetryModules.Count());
66+
Assert.Equal(8, telemetryModules.Count());
67+
68+
Assert.Single(telemetryModules.Where(m => m.GetType() == typeof(DiagnosticsTelemetryModule)));
5969
}
6070

6171
[Theory]

NETCORE/test/Microsoft.ApplicationInsights.WorkerService.Tests/ExtensionsTest.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,22 @@ private static ServiceCollection CreateServicesAndAddApplicationinsightsWorker(A
123123
return services;
124124
}
125125

126-
[Fact]
127-
public static void TelemetryModulesResolvableWhenKeyedServiceRegistered()
126+
[Theory]
127+
[InlineData(true)]
128+
[InlineData(false)]
129+
public static void TelemetryModulesResolvableWhenKeyedServiceRegistered(bool manuallyRegisterDiagnosticsTelemetryModule)
128130
{
129131
// Note: This test verifies a regression doesn't get introduced for:
130-
// https://github.com/dotnet/extensions/issues/5222
132+
// https://github.com/microsoft/ApplicationInsights-dotnet/issues/2879
131133

132134
var services = new ServiceCollection();
133135

136+
services.AddSingleton<ITelemetryModule, TestTelemetryModule>();
137+
if (manuallyRegisterDiagnosticsTelemetryModule)
138+
{
139+
services.AddSingleton<ITelemetryModule, DiagnosticsTelemetryModule>();
140+
}
141+
134142
services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationType: typeof(TestService));
135143
services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationInstance: new TestService());
136144
services.AddKeyedSingleton(typeof(ITestService), serviceKey: new(), implementationFactory: (sp, key) => new TestService());
@@ -141,7 +149,9 @@ public static void TelemetryModulesResolvableWhenKeyedServiceRegistered()
141149

142150
var telemetryModules = sp.GetServices<ITelemetryModule>();
143151

144-
Assert.Equal(7, telemetryModules.Count());
152+
Assert.Equal(8, telemetryModules.Count());
153+
154+
Assert.Single(telemetryModules.Where(m => m.GetType() == typeof(DiagnosticsTelemetryModule)));
145155
}
146156

147157
[Theory]
@@ -921,6 +931,13 @@ private sealed class TestService : ITestService
921931
{
922932
}
923933

934+
private sealed class TestTelemetryModule : ITelemetryModule
935+
{
936+
public void Initialize(TelemetryConfiguration configuration)
937+
{
938+
}
939+
}
940+
924941
private interface ITestService
925942
{
926943
}

0 commit comments

Comments
 (0)