Skip to content

Fix request exception in WEB package#3184

Merged
harsimar merged 6 commits into
mainfrom
harskaur/requestExFix
May 28, 2026
Merged

Fix request exception in WEB package#3184
harsimar merged 6 commits into
mainfrom
harskaur/requestExFix

Conversation

@harsimar
Copy link
Copy Markdown
Member

This PR fixes the below exception:

Request is not available in this context Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Request is not available in this context
Source Error:
Line 17:         public static HttpRequest GetRequest(this HttpContext context) Line 18:         { Line 19:             return context?.Request; Line 20:         } Line 21:
Source File: C:\repos\ApplicationInsights-dotnet\WEB\Src\Web\Web\Implementation\HttpContextExtensions.cs    Line: 19

Encountered when one calls TrackDependency inside Application_Start method.

Copilot AI review requested due to automatic review settings May 27, 2026 17:45
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes an HttpException ("Request is not available in this context") that occurred when telemetry was generated outside a request scope (e.g., TrackDependency in Application_Start). The GetRequest extension on HttpContext is hardened to catch this exception and return null, and all activity processors are updated to use the safe accessor.

Changes:

  • Wrap HttpContext.Request access in HttpContextExtensions.GetRequest with a try/catch for HttpException, returning null instead.
  • Update all activity processors (AccountIdActivityProcessor, AuthenticatedUserIdActivityProcessor, ClientIpHeaderActivityProcessor, SessionActivityProcessor, UserActivityProcessor) to call context.GetRequest() and short-circuit when null.
  • Update CreateRequestNamePrivate to use the same safe accessor.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
WEB/Src/Web/Web/Implementation/HttpContextExtensions.cs Hardens GetRequest against HttpException; updates docs; refactors CreateRequestNamePrivate to use it.
WEB/Src/Web/Web/AccountIdActivityProcessor.cs Uses safe GetRequest() accessor in OnEnd.
WEB/Src/Web/Web/AuthenticatedUserIdActivityProcessor.cs Uses safe GetRequest() accessor in OnEnd.
WEB/Src/Web/Web/ClientIpHeaderActivityProcessor.cs Uses safe GetRequest() accessor; updates request reads to local variable.
WEB/Src/Web/Web/SessionActivityProcessor.cs Uses safe GetRequest() accessor in OnEnd.
WEB/Src/Web/Web/UserActivityProcessor.cs Uses safe GetRequest() accessor in OnEnd.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread WEB/Src/Web/Web/AccountIdActivityProcessor.cs Dismissed
Comment thread WEB/Src/Web/Web/AuthenticatedUserIdActivityProcessor.cs Dismissed
@harsimar harsimar merged commit 5dce78e into main May 28, 2026
19 checks passed
@harsimar harsimar deleted the harskaur/requestExFix branch May 28, 2026 20:08
This was referenced May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants