Calling AddApplicationInsightsTelemetry() after .Decorate<,> removes TelemetryClient from service collection
Closed this issue · 4 comments
I'm experiencing a strange issue in my projects after upgrading package version from 4.2.2 to 5.0.1.
I created the basic weather app web application template to replicate:
using Microsoft.ApplicationInsights;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IFake, FakeService>();
builder.Services.Decorate<IFake, FakeDecorateService>();
builder.Services.AddApplicationInsightsTelemetry();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", (IFake fakeService, TelemetryClient telemetryClient) =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
public interface IFake
{
int Something();
}
public class FakeService : IFake
{
public int Something()
{
return 1;
}
}
public class FakeDecorateService(IFake fakeService) : IFake
{
public int Something()
{
return fakeService.Something();
}
}
Calling the weather endpoint results in an exception because the TelemetryClient was not registered.
Note that calling builder.Services.AddApplicationInsightsTelemetry();
before .Decorate<,>
works.
Hmm. That's weird. It would be interesting to see what the contents of builder.Services
looks like in the following spots:
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
< -- here -- >
builder.Services.AddScoped<IFake, FakeService>();
builder.Services.Decorate<IFake, FakeDecorateService>();
< -- here -- >
builder.Services.AddApplicationInsightsTelemetry();
< -- here -- >
var app = builder.Build();
results in an exception because the TelemetryClient was not registered.
What does the exception say? Which type is it trying to resolve, exactly?
I have had the same issue - added Scrutor to my API and Application Insights stopped working. Moving AddApplicationInsightsTelemetry
to before decorating fixed the issue - thanks.
@khellang we had the same problem here. Applied the same strategy to register ApplicationInsight before the services. It worked.
.Net8
Scrutor 5.0.2
Ok, so apparenly ApplicationInsights has an internal method called AddSingletonIfNotExists
, which doesn't support keyed services, which Scrutor leverages for decoration from 5.0.0. This results in a NullReferenceException
, which ends up being swallowed in AddApplicationInsightsTelemetry
- resulting in a half-registered app insights setup.
See See microsoft/ApplicationInsights-dotnet#2879 for more details.