Skip to content

Dependency Injection validation issue with IHttpMessageHandlerBuilderFilter in Sentry 3.0.2 #785

@Turnerj

Description

@Turnerj

Environment

How do you use Sentry?
Sentry SaaS (sentry.io)

Which SDK and version?
.NET 3.0.2 (running on .NET 5)

Steps to Reproduce

  1. Create a new/blank ASP.NET Core project for .NET 5.
  2. 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();
			});
}
  1. 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!");
			});
		});
	}
}
  1. 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.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions