Fix NullReferenceException in WorkflowHost with .NET 8+ dependency injection#1404
Draft
Fix NullReferenceException in WorkflowHost with .NET 8+ dependency injection#1404
Conversation
- Changed GetService to GetRequiredService for ILifeCycleEventPublisher registration - Added null-safety checks when iterating background tasks in StartAsync - Added null-safety checks when iterating background tasks in StopAsync Co-authored-by: danielgerlag <2357007+danielgerlag@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix object reference error on NET 10 upgrade
Fix NullReferenceException in WorkflowHost with .NET 8+ dependency injection
Nov 13, 2025
Owner
|
@copilot Did you test with actual .net 10 and the SQL persistence provider? |
When OpenTelemetry tracing is not enabled, WorkflowActivity.StartHost() returns null. Added null-conditional operator when calling activity.AddException() to prevent NullReferenceException. Co-authored-by: danielgerlag <2357007+danielgerlag@users.noreply.github.com>
Contributor
Author
Yes, I've now tested with actual .NET 10 and SQL Server persistence provider. The test revealed an additional NullReferenceException that occurred when OpenTelemetry tracing is not enabled - Fixed in commit e36b2ad by adding the null-conditional operator: Test results with .NET 10 + SQL Server persistence:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe the change
Fixes
NullReferenceExceptionoccurring when WorkflowHost starts on .NET 8+ with newer Microsoft.Extensions.DependencyInjection versions (8.0+, 9.0+, 10.0+).Describe your implementation or design
Two separate issues were causing NullReferenceException:
DI Registration Issue: Using
GetService<T>()instead ofGetRequiredService<T>()when registering ILifeCycleEventPublisher as an IBackgroundTask meant that if the service failed to resolve,nullwould be added to theIEnumerable<IBackgroundTask>collection, causing a NullReferenceException when iterating.Activity Tracing Issue: When OpenTelemetry tracing is not enabled,
WorkflowActivity.StartHost()returnsnull. The exception handler inStartAsyncwas callingactivity.AddException(ex)without checking for null, causing a NullReferenceException.Changes:
sp.GetService<ILifeCycleEventPublisher>()tosp.GetRequiredService<ILifeCycleEventPublisher>()for fail-fast behavior with clear error messagesStartAsync()andStopAsync()as defensive programmingactivity.AddException(ex)toactivity?.AddException(ex)to handle null activity when OpenTelemetry tracing is disabledTests
Verified with .NET 10 runtime and Microsoft.Extensions.DependencyInjection 10.0.0:
Breaking change
No. If ILifeCycleEventPublisher cannot be resolved (which shouldn't happen in valid configurations), the code now throws a clear exception during service registration instead of a cryptic runtime error—this is an improvement in error reporting.
Additional context
The fail-fast approach provides better diagnostics for misconfigured DI containers while the null checks provide defense-in-depth. The activity null-check ensures compatibility whether OpenTelemetry tracing is enabled or not.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.