Class ContextUtils
Contains utility methods for capturing system's ambient context and propagating it to methods which are executed asynchronously or in parallel.
public static class ContextUtils
- Inheritance
-
objectContextUtils
Methods
PropagateCurrent(Action)
Captures the current system's ambient context and wraps action so that the context is propagated to it upon its invocation.
public static Action PropagateCurrent(Action action)
Parameters
actionActionAction to which the current context is to be propagated.
Returns
- Action
Returns
actionwrapped so that current context is propagated to it upon invocation.
Remarks
Use this method to wrap actions before passing them to System.Threading.Tasks.Task.Run(System.Action) or System.Threading.Tasks.Task.Run(System.Action, System.Threading.CancellationToken) to ensure the current context is captured and propagated to the executed action.
Exceptions
- ArgumentNullException
Thrown when
actionis null.
PropagateCurrent(Func<Task>)
Captures the current system's ambient context and wraps function so that the context is propagated to it upon its invocation.
public static Func<Task> PropagateCurrent(Func<Task> function)
Parameters
functionFunc<Task>Asynchronous action to which the current context is to be propagated.
Returns
- Func<Task>
Returns
functionwrapped so that current context is propagated to it upon invocation.
Remarks
Use this method to wrap asynchronous actions before passing them to System.Threading.Tasks.Task.Run(System.Func<System.Threading.Tasks.Task>) or System.Threading.Tasks.Task.Run(System.Func<System.Threading.Tasks.Task>, System.Threading.CancellationToken) to ensure the current context is captured and propagated to the executed action.
Exceptions
- ArgumentNullException
Thrown when
functionis null.
PropagateCurrent<TResult>(Func<Task<TResult>>)
Captures the current system's ambient context and wraps function so that the context is propagated to it upon its invocation.
public static Func<Task<TResult>> PropagateCurrent<TResult>(Func<Task<TResult>> function)
Parameters
functionFunc<Task<TResult>>Asynchronous function to which the current context is to be propagated.
Returns
- Func<Task<TResult>>
Returns
functionwrapped so that current context is propagated to it upon invocation.
Type Parameters
TResultType of
function's result.
Remarks
Use this method to wrap asynchronous functions before passing them to System.Threading.Tasks.Task.Run<TResult>(System.Func<System.Threading.Tasks.Task<TResult>>) or System.Threading.Tasks.Task.Run<TResult>(System.Func<System.Threading.Tasks.Task<TResult>>, System.Threading.CancellationToken) to ensure the current context is captured and propagated to the executed function.
Exceptions
- ArgumentNullException
Thrown when
functionis null.
PropagateCurrent<TResult>(Func<TResult>)
Captures the current system's ambient context and wraps function so that the context is propagated to it upon its invocation.
public static Func<TResult> PropagateCurrent<TResult>(Func<TResult> function)
Parameters
functionFunc<TResult>Function to which the current context is to be propagated.
Returns
- Func<TResult>
Returns
functionwrapped so that current context is propagated to it upon invocation.
Type Parameters
TResultType of
function's result.
Remarks
Use this method to wrap functions before passing them to System.Threading.Tasks.Task.Run<TResult>(System.Func<TResult>) or System.Threading.Tasks.Task.Run<TResult>(System.Func<TResult>, System.Threading.CancellationToken) to ensure the current context is captured and propagated to the executed function.
Exceptions
- ArgumentNullException
Thrown when
functionis null.
ResetCurrent()
Resets the current system's ambient context.
public static void ResetCurrent()
Remarks
Use this method within an action when the context was not explicitly captured and passed (e.g. when running on the thread pool where residual context from previous code might linger).