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

Calling AddApplicationInsightsTelemetry() after .Decorate<,> removes TelemetryClient from service collection #233

Open
Simkortet opened this issue Oct 17, 2024 · 3 comments

Comments

@Simkortet
Copy link

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.

@khellang
Copy link
Owner

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?

@thomasread99
Copy link

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.

@marco-olimpio
Copy link

@khellang we had the same problem here. Applied the same strategy to register ApplicationInsight before the services. It worked.
.Net8
Scrutor 5.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants