From 1447b5598a6c5b23bffc7e3bb19deadb98088acc Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Wed, 5 Jun 2024 19:20:39 +1200 Subject: [PATCH] Fixed null IServiceProvider in anonymous routes with OpenTelemetry (#3401) --- CHANGELOG.md | 4 ++++ src/Sentry.AspNetCore/ScopeExtensions.cs | 5 ++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 691981868c..df138d36ea 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Fixes + +- Fixed null IServiceProvider in anonymous routes with OpenTelemetry ([#3401](https://github.com/getsentry/sentry-dotnet/pull/3401)) + ### Dependencies - Bump CLI from v2.31.2 to v2.32.1 ([#3398](https://github.com/getsentry/sentry-dotnet/pull/3398)) diff --git a/src/Sentry.AspNetCore/ScopeExtensions.cs b/src/Sentry.AspNetCore/ScopeExtensions.cs index a8e33677a7..dcddf56ded 100644 --- a/src/Sentry.AspNetCore/ScopeExtensions.cs +++ b/src/Sentry.AspNetCore/ScopeExtensions.cs @@ -38,7 +38,10 @@ public static void Populate(this Scope scope, HttpContext context, SentryAspNetC if (options.SendDefaultPii && !scope.HasUser()) { - var userFactory = context.RequestServices.GetService(); + // Although the compiler suggests context.RequestServices cannot be null, in practice it can be, so we + // have a null check here. + // See: https://github.com/getsentry/sentry-dotnet/issues/3395 + var userFactory = context.RequestServices?.GetService(); var user = userFactory?.Create(); if (user != null)