Dependency Injection validation issue with IHttpMessageHandlerBuilderFilter in Sentry 3.0.2
See original GitHub issueEnvironment
How do you use Sentry? Sentry SaaS (sentry.io)
Which SDK and version? .NET 3.0.2 (running on .NET 5)
Steps to Reproduce
- Create a new/blank ASP.NET Core project for .NET 5.
- In
Program.cs
, have the following:
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.UseStartup<Startup>()
.UseSentry();
});
}
- In
Startup.cs
have the following:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpClient(); // This line causes conflict with Sentry 3.0.2 (works fine in 3.0.1)
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
- Try to debug the application
Expected Result
It would work? Everything about the patch seemed like it would just run fine. 🤷♂️
Actual Result
An exception is thrown during startup when debugging as Microsoft Dependency Injection has validation enabled by default during development/debugging.
System.AggregateException: ‘Some services are not able to be constructed (Error while validating the service descriptor ‘ServiceType: Microsoft.Extensions.Http.DefaultHttpClientFactory Lifetime: Singleton ImplementationType: Microsoft.Extensions.Http.DefaultHttpClientFactory’: Cannot consume scoped service ‘System.Collections.Generic.IEnumerable`1[Microsoft.Extensions.Http.IHttpMessageHandlerBuilderFilter]’ from singleton ‘Microsoft.Extensions.Http.DefaultHttpClientFactory’.)’
Notes
The issue seems to relate to changes in #784, specifically this line: https://github.com/getsentry/sentry-dotnet/pull/784/files#diff-7d117571d3f4e39dc910da7ee00ab9691f575b9e3064a02cb9d1f689cae5d188R37-R40
At this stage, I haven’t been able to isolate the issue any further than this. It seems to be conflicting with some use of injecting a HttpClient
or IHttpClientFactory
somewhere in my own ConfigureServices
method in my startup class.
I’ll post more details when I have them!
EDIT: The issue seems to stem from any injection of a HttpClient
and/or calling AddHttpClient
myself in my code. Going to look to see if I can write a test for the repo that can trigger this issue too.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top GitHub Comments
@Turnerj no problem. The fix should be included in version 3.0.3 deployed today.
https://github.com/getsentry/sentry-dotnet/releases/tag/3.0.3
Hey @Tyrrrz - thanks for getting this sorted so fast!