ApplicationInsights-dotnet icon indicating copy to clipboard operation
ApplicationInsights-dotnet copied to clipboard

Issue when updating Microsoft.ApplicationInsights.AspNetCore from v2.22.0 to v2.23.0 (all other things kept unchanged)

Open frodesorhoy opened this issue 9 months ago • 3 comments

        <PackageReference Include="Azure.Messaging.ServiceBus" Version="7.18.3" />
        <PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
        <PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.11.0" />
        <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.1" />
        <PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.23.0" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="9.0.1" />
        <PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.1" />
        <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.1" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.1" />
        <PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.1.0" />
        <PackageReference Include="Microsoft.IdentityModel.Protocols.OpenIdConnect" Version="8.3.1" />
        <PackageReference Include="Microsoft.IdentityModel.Tokens.Saml" Version="8.3.1" />
        <PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.1" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
        <PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.1" />
        <PackageReference Include="Microsoft.Extensions.Logging.AzureAppServices" Version="9.0.1" />
        <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
        <PackageReference Include="Polly.Extensions" Version="8.5.1" />
        <PackageReference Include="prometheus-net.AspNetCore" Version="8.2.1" />
        <PackageReference Include="Serilog" Version="4.2.0" />
        <PackageReference Include="Serilog.AspNetCore" Version="9.0.0" />
        <PackageReference Include="Serilog.Settings.Configuration" Version="9.0.0" />
        <PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
        <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.3.1" />
        <PackageReference Include="System.ServiceModel.Duplex" Version="6.0.0" />
        <PackageReference Include="System.ServiceModel.Http" Version="8.1.1" />
        <PackageReference Include="System.ServiceModel.Primitives" Version="8.1.1" />
  • Runtime version (e.g. net461, net48, netcoreapp2.1, netcoreapp3.1, etc. You can find this information from the *.csproj file):
        <TargetFramework>net9.0</TargetFramework>
  • Hosting environment (e.g. Azure Web App, App Service on Linux, Windows, Ubuntu, etc.):
  • Windows
  • Linux

Describe the bug

After upgrading to recent version we get the following error when trying to get a service from the provider:

Message = "Unable to resolve service for type 'Microsoft.AspNetCore.Hosting.IHostingEnvironment' while attempting to activate 'Microsoft.AspNetCore.Hosting.DefaultApplicationInsightsServiceConfigureOptions'."

It is the following lines that introduces the error: services.AddApplicationInsightsTelemetry(applicationInsightsServiceOptions);

When I remove the same line the rest of the code works, and no exceptions, but I think we then will loose Telemetry logging.

Has the method of initalization, registering the services, or dependencieschanged from 2.22.0 to 2.23.0?

frodesorhoy avatar Feb 21 '25 10:02 frodesorhoy

Hi @frodesorhoy, There was only one change to how services are registered. https://github.com/microsoft/ApplicationInsights-dotnet/pull/2908 This was a fix for another bug.

Yours is the first bug we've had reported with the 2.23.0 milestone. If you can please share a minimal repro for this issue I would really like to take a look.

TimothyMothra avatar Feb 21 '25 16:02 TimothyMothra

I got a workaround (in our case):

Before (where I get the error):

       services.AddApplicationInsightsTelemetry();
       services.AddSingleton(_ =>
       {
           var telemetryConfiguration = new TelemetryConfiguration();
           telemetryConfiguration.ConnectionString = configuration["ApplicationInsights:ConnectionString"];
           telemetryConfiguration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
           return new TelemetryClient(telemetryConfiguration);
       });

After (no errors):

       services.AddSingleton(_ =>
       {
           var telemetryConfiguration = new TelemetryConfiguration();
           telemetryConfiguration.ConnectionString = configuration["ApplicationInsights:ConnectionString"];
           telemetryConfiguration.TelemetryInitializers.Add(new OperationCorrelationTelemetryInitializer());
           return new TelemetryClient(telemetryConfiguration);
       });
       services.AddApplicationInsightsTelemetry();

There seems to be some dependencies on the order of the operations and how implementations and interfaces are registered. We've had similar issues in other projects where the order of the registrations have had impact on the if Application Insights have worked or not. There we have fixed the issues by registering Application Insights before all else. Working a litle "by accident" I guess :-)

frodesorhoy avatar Feb 23 '25 13:02 frodesorhoy

We encountered the issue. Thanks @frodesorhoy for the workaround

busesorin94 avatar Mar 17 '25 10:03 busesorin94