Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ITelemetryModule singleton registration to support presence of keyed services #2908

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 1 addition & 21 deletions NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,26 +280,6 @@ internal static void AddTelemetryConfiguration(
}
}

/// <summary>
/// The AddSingleton method will not check if a class has already been added as an ImplementationType.
/// This extension method is to encapsulate those checks.
/// </summary>
/// <remarks>
/// Must check all three properties to avoid duplicates or null ref exceptions.
/// </remarks>
/// <typeparam name="TService">The type of the service to add.</typeparam>
/// <typeparam name="TImplementation">The type of the implementation to use.</typeparam>
/// <param name="services">The Microsoft.Extensions.DependencyInjection.IServiceCollection to add the service to.</param>
internal static void AddSingletonIfNotExists<TService, TImplementation>(this IServiceCollection services)
where TService : class
where TImplementation : class, TService
{
if (!services.Any(o => o.ImplementationFactory == null && typeof(TImplementation).IsAssignableFrom(o.ImplementationType ?? o.ImplementationInstance.GetType())))
{
services.AddSingleton<TService, TImplementation>();
}
}

private static bool TryGetValue(this IConfiguration config, string primaryKey, out string value, string backupKey = null)
{
value = config[primaryKey];
Expand Down Expand Up @@ -332,7 +312,7 @@ private static void AddCommonInitializers(IServiceCollection services)
private static void AddCommonTelemetryModules(IServiceCollection services)
{
// Previously users were encouraged to manually add the DiagnosticsTelemetryModule.
services.AddSingletonIfNotExists<ITelemetryModule, DiagnosticsTelemetryModule>();
services.TryAddEnumerable(ServiceDescriptor.Singleton<ITelemetryModule, DiagnosticsTelemetryModule>());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimothyMothra FYI I didn't make any other changes here but really all of the services.AddSingleton<ITelemetryModule, ...>(); calls should be TryAddEnumerable calls. If user only calls registration once everything will be fine. But if they call one of the "Add" extensions more than once, they'll get duplicates of all of these modules. Typically "Add" extensions should be safe to be called more than once which is accomplished by calling TryAddSingleton \ TryAddEnumerable 🤷

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the full history, but I reviewed all my previous commit comments.

It looks like I was trying to add safety checks for every possible way that a user could have added the DiagnosticsTelemetryModule (link 1)(link 2). The TryAdd is substantially better :)

Thank you


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

Large diffs are not rendered by default.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
</PackageReference>
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
</ItemGroup>

<ItemGroup Condition="'$(IsNetCore)' == 'True'">
<!-- TODO: Can't switch to FrameworkReference yet;
'IHostingEnvironment' is obsolete: 'This type is obsolete and will be removed in a future version.
<!-- TODO: Can't switch to FrameworkReference yet;
'IHostingEnvironment' is obsolete: 'This type is obsolete and will be removed in a future version.
The recommended alternative is Microsoft.AspNetCore.Hosting.IWebHostEnvironment.' -->
<!--<FrameworkReference Include="Microsoft.AspNetCore.App" />-->

Expand Down
Loading
Loading