This commit is contained in:
Mikel Blanchard 2024-10-23 11:53:26 -07:00
Родитель d5f7d8bd36
Коммит 42cd7e3a6d
4 изменённых файлов: 37 добавлений и 9 удалений

Просмотреть файл

@ -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.

Просмотреть файл

@ -312,7 +312,7 @@
private static void AddCommonTelemetryModules(IServiceCollection services)
{
// Previously users were encouraged to manually add the DiagnosticsTelemetryModule.
services.TryAddSingleton<ITelemetryModule, DiagnosticsTelemetryModule>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITelemetryModule, DiagnosticsTelemetryModule>());
// These modules add properties to Heartbeat and expect the DiagnosticsTelemetryModule to be configured in DI.
services.AddSingleton<ITelemetryModule, AppServicesHeartbeatTelemetryModule>();

Просмотреть файл

@ -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<ITelemetryModule, TestTelemetryModule>();
if (manuallyRegisterDiagnosticsTelemetryModule)
{
services.AddSingleton<ITelemetryModule, DiagnosticsTelemetryModule>();
}
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<ITelemetryModule>();
Assert.Equal(7, telemetryModules.Count());
Assert.Equal(8, telemetryModules.Count());
Assert.Single(telemetryModules.Where(m => m.GetType() == typeof(DiagnosticsTelemetryModule)));
}
[Theory]

Просмотреть файл

@ -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<ITelemetryModule, TestTelemetryModule>();
if (manuallyRegisterDiagnosticsTelemetryModule)
{
services.AddSingleton<ITelemetryModule, DiagnosticsTelemetryModule>();
}
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<ITelemetryModule>();
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
{
}