In AddApplicationInsightsKubernetesEnricher method there is a check if initalizer already exists: KubernetesTelemetryInitializerExists. Implementation of that method checks if any ImplementationType from service collection is of type KubernetesTelemetryInitializer but getting ImplementationType property by design throws exception if it's keyed service.
/// <summary>
/// Gets the <see cref="Type"/> that implements the service.
/// </summary>
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
public Type? ImplementationType
{
get
{
if (IsKeyedService)
{
ThrowKeyedDescriptor();
}
return _implementationType;
}
}
//
// Summary:
// Checks if the KubernetesTelemetryInitializer exists in the service collection.
//
//
// Parameters:
// serviceCollection:
// The service collection.
private static bool KubernetesTelemetryInitializerExists(IServiceCollection serviceCollection)
{
return serviceCollection.Any((ServiceDescriptor t) => t.ImplementationType == typeof(KubernetesTelemetryInitializer));
}
Describe the bug Can't register Enrichers when using KeyedServices when using .NET 8
Package Versions Application Insights Kubernetes Version: 6.1.1 Application Insights SDK Version: 2.22.0
To Reproduce Steps to reproduce the behavior:
Expected behavior It is possible to add Kubernetes enrichers when you used keyed services.
Screenshots Stacktrace:![image](https://github.com/microsoft/ApplicationInsights-Kubernetes/assets/13612984/47255677-88ea-44aa-b8e1-e1c96c1621ab)
Additional context
https://github.com/AzureAD/microsoft-identity-web/issues/2604
In AddApplicationInsightsKubernetesEnricher method there is a check if initalizer already exists: KubernetesTelemetryInitializerExists. Implementation of that method checks if any ImplementationType from service collection is of type KubernetesTelemetryInitializer but getting ImplementationType property by design throws exception if it's keyed service.