ExecutionContext
What Is It?
Contextual data that automatically flows across async operations. Unlike SynchronizationContext (which is platform-specific), ExecutionContext flows in all .NET applications.
What Flows in ExecutionContext?
1. Thread.CurrentCulture and Thread.CurrentUICulture
Culture settings for formatting dates, numbers, and UI text. Ensures your async code respects user's locale settings even when resuming on different threads.
2. AsyncLocal<T> values
Thread-local storage that flows across async boundaries. Used for correlation IDs, request context, logging context, etc. Each async flow gets its own isolated copy.
3. Security principal/identity
The current user's identity (Thread.CurrentPrincipal). Critical for authorization checks in async code - the user context is preserved across awaits.
4. Other ambient data
Impersonation context, host execution context, and other framework-specific data that needs to flow with async operations.
βΉοΈ Key Point: ExecutionContext is separate from SynchronizationContext. ExecutionContext flows automatically in all apps (console, web, UI); you rarely need to think about it.
π‘ Guarantee: These values are preserved across awaits, even when resuming on a different thread. The .NET runtime handles this automatically.