question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dependency Injection validation issue with IHttpMessageHandlerBuilderFilter in Sentry 3.0.2

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
Tyrrrzcommented, Feb 4, 2021

@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

2reactions
Turnerjcommented, Feb 4, 2021

Hey @Tyrrrz - thanks for getting this sorted so fast!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ASP.NET Core
Dependency Injection. Much of the behavior of the ASP.NET Core integration with Sentry can be customized by using the frameworks dependency injection system....
Read more >
New in ASP.NET Core 3: Service provider validation
1. Controller constructor dependencies aren't checked. The reason the validation feature doesn't catch this problem is that controllers aren't ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found