This repository explores advanced concepts in asynchronous programming in C#, focusing on practical implementations and patterns that enhance application performance and maintainability. Here, you'll find detailed examples and in-depth discussions on managing execution contexts and maintaining state across asynchronous calls using ExecutionContext and AsyncLocal.
-
ExecutionContext Management: Demonstrates how to manually capture and flow the execution context across threads to ensure that environmental data such as security information, culture settings, and call context data is preserved accurately across asynchronous operations.
-
AsyncLocal Usage: Provides examples of using AsyncLocal for storing and flowing data automatically with the execution context across asynchronous calls and thread switches, ideal for scenarios that require thread-specific data isolation.
-
Combined Use Cases: Showcases how ExecutionContext and AsyncLocal can be combined to handle complex scenarios like tracking user sessions, managing security tokens, or providing operation-specific logging that persists across asynchronous operations and thread boundaries.
-
Practical Examples: Includes practical examples such as tracking operation IDs in a web application to maintain a consistent context for logging and diagnostics across asynchronous method calls and nested tasks.
To dive into the examples, clone this repository to your local machine:
git clone https://github.com/kai-Tr/advanced-async-programming-csharp.git
Navigate to the repository directory and explore the various C# scripts that demonstrate each concept.
Contributions to enhance the examples or extend the discussions are welcome. Please fork the repository, make your changes, and submit a pull request. For major changes or new examples, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.