Skip to content

Fix TelemetryClient.Flush/FlushAsync NullReferenceException in DI scenarios #3125

Merged
rajkumar-rangaraj merged 4 commits intomainfrom
rajrang/fixFlushForDI
Feb 20, 2026
Merged

Fix TelemetryClient.Flush/FlushAsync NullReferenceException in DI scenarios #3125
rajkumar-rangaraj merged 4 commits intomainfrom
rajrang/fixFlushForDI

Conversation

@rajkumar-rangaraj
Copy link
Copy Markdown
Member

Fix Issue #3115 .

Changes

TelemetryClient.Flush() and FlushAsync() throw System.NullReferenceException when called from DI scenarios because this.sdk is null.

In DI scenarios, TelemetryClient is constructed with isFromDependencyInjection: true, which intentionally skips calling TelemetryConfiguration.Build() — the OTel SDK is owned by AddOpenTelemetry() in the DI container, not by TelemetryConfiguration. However, both Flush() and FlushAsync() unconditionally access this.sdk.TracerProvider, causing:

System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.ApplicationInsights.TelemetryClient.<>c__DisplayClass36_0.<FlushAsync>b__0()
  • Added an IServiceProvider field and a new internal constructor that accepts it
  • Updated the DI TelemetryClient factory in AddTelemetryConfigAndClient to pass the IServiceProvider
  • Flush() and FlushAsync() now check: if this.sdk is available (non-DI), flush via the SDK; otherwise if this.serviceProvider is available (DI), resolve TracerProvider/MeterProvider/LoggerProvider from the DI container and flush those

Copilot AI review requested due to automatic review settings February 20, 2026 18:12
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

This pull request fixes a NullReferenceException in TelemetryClient.Flush() and FlushAsync() methods when called in dependency injection (DI) scenarios. The issue occurred because in DI scenarios, TelemetryClient is constructed with isFromDependencyInjection: true, which intentionally skips building the OpenTelemetry SDK (since it's managed by the DI container via AddOpenTelemetry()). However, both flush methods unconditionally accessed this.sdk.TracerProvider, causing a NullReferenceException when sdk was null.

Changes:

  • Added IServiceProvider field to TelemetryClient and a new internal constructor that accepts it
  • Updated DI registration to pass the IServiceProvider to TelemetryClient
  • Modified Flush() and FlushAsync() to check if SDK is available first, then fall back to resolving providers from DI container

Reviewed changes

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

File Description
BASE/src/Microsoft.ApplicationInsights/TelemetryClient.cs Added serviceProvider field, new constructor accepting IServiceProvider, and updated Flush()/FlushAsync() to resolve OTel providers from DI when sdk is null
NETCORE/src/Shared/Extensions/ApplicationInsightsExtensions.cs Updated TelemetryClient factory registration to pass IServiceProvider to the new constructor

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

@rajkumar-rangaraj rajkumar-rangaraj changed the title Fix TelemetryClient.Flush/FlushAsync NullReferenceException in DI scenarios ## Problem Fix TelemetryClient.Flush/FlushAsync NullReferenceException in DI scenarios Feb 20, 2026
Copy link
Copy Markdown
Member

@harsimar harsimar left a comment

Choose a reason for hiding this comment

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

How was this tested? Consider adding some automated testing

@rajkumar-rangaraj
Copy link
Copy Markdown
Member Author

How was this tested? Consider adding some automated testing

We don't have an automated infra built yet. It's outside the scope of the PR. We might need to consider building one.
I tested using the repro provided by customer.

@rajkumar-rangaraj rajkumar-rangaraj merged commit a04ee19 into main Feb 20, 2026
18 checks passed
@rajkumar-rangaraj rajkumar-rangaraj deleted the rajrang/fixFlushForDI branch February 20, 2026 21:19
@rajkumar-rangaraj rajkumar-rangaraj restored the rajrang/fixFlushForDI branch February 20, 2026 21:21
@maarten-kieft
Copy link
Copy Markdown

I'm running in this issue; what is the expected release date of this fix?

@rajkumar-rangaraj
Copy link
Copy Markdown
Member Author

I'm running in this issue; what is the expected release date of this fix?

Within 2 weeks.

This was referenced Apr 6, 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