Fix NullReferenceException in WorkflowHost with .NET 8+ dependency injection#1404
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 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>
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:
|
|
Hi @danielgerlag |
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.