Skip to content

Commit

Permalink
Fixed null IServiceProvider in anonymous routes with OpenTelemetry (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrosswell committed Jun 5, 2024
1 parent aa60fc1 commit 1447b55
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion src/Sentry.AspNetCore/ScopeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public static void Populate(this Scope scope, HttpContext context, SentryAspNetC

if (options.SendDefaultPii && !scope.HasUser())
{
var userFactory = context.RequestServices.GetService<ISentryUserFactory>();
// 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<ISentryUserFactory>();
var user = userFactory?.Create();

if (user != null)
Expand Down

0 comments on commit 1447b55

Please sign in to comment.