dotnet / dotnet/aspnetcore

[SignalR] Blazor's Activity.Current is null when executing a HubMethod

Open
#63,313 1 comment 0 reactions 0 assignees View on GitHub
area-signalr
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

In PR [#57049](https://github.com/dotnet/aspnetcore/pull/57049), a change was made to set Activity.Current to null prior to the execution of dispatcher.ExecuteHubMethod in SignalR. This modification has resulted in a breaking change for telemetry correlation in Application Insights.
Impact on Application Insights telemetry:

- The operation_Id field in Application Insights is now null instead of containing the expected correlation ID
- The operation_ParentId field in Application Insights is now null instead of containing the parent activity ID
- This breaks telemetry correlation chains, making it difficult to trace requests across distributed systems

Root cause: The .NET SDK for Application Insights relies on Activity.Current to establish telemetry correlation. When Activity.Current is set to null before hub method execution, the Application Insights SDK cannot capture the proper correlation context, resulting in missing operation_Id and operation_ParentId values.

I have also tested .NET 10 preview 6 and the bug is still there.

### Expected Behavior

- operation_Id should contain the trace ID from the current activity
- operation_ParentId should contain the span ID from the parent activity
- Telemetry correlation should be maintained throughout the SignalR hub method execution

### Steps To Reproduce

- Clone [this repo ](https://github.com/Hamata6/DemoForAppInsightsNet9Server). It is a newly created Blazor Web app on .NET 9 with only sample code on the Home page.
- Add Application Insights in Visual Studio via Project > Add Application Insights Telemetry > Azure Application Insights.
- Run solution and click button on homepage to trigger a TrackException.
- Check your Application Insights exception logs to see that there is no operation_Id and operation_ParentId:
`exceptions
| where outerMessage == "Demo Exception for Activity.Current issue"
| project timestamp, outerMessage, operation_Id, operation_ParentId`

### Exceptions (if any)

_No response_

### .NET Version

Since 9.0.0 through 10.0.0 preview 6

### Anything else?

Output on .NET 9:
Image

Output on .NET 8:
Image

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.