using System.Threading; using System.Threading.Tasks; async Task MyMethodAsync() { var codeContext = ExecutionContext.Capture(); // Code to execute in this context }
using System.Threading; using System.Threading.Tasks; async Task MyMethodAsync() { var codeContext = ExecutionContext.Capture(); await Task.Run(() => MyMethodInTask(codeContext)); } void MyMethodInTask(object state) { var codeContext = state as ExecutionContext; // Code to execute in this context }In this example, we pass the CodeContext Entry to a new task using the Run method of the Task class. This allows us to execute code in a different context while preserving the original context. Package Library: 'System.Threading.Tasks.Extensions'