Table of Contents

Class ContextUtils

Namespace
CMS.Base
Assembly
CMS.Base.dll

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
object
ContextUtils

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

action Action

Action to which the current context is to be propagated.

Returns

Action

Returns action wrapped 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 action is 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

function Func<Task>

Asynchronous action to which the current context is to be propagated.

Returns

Func<Task>

Returns function wrapped 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 function is 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

function Func<Task<TResult>>

Asynchronous function to which the current context is to be propagated.

Returns

Func<Task<TResult>>

Returns function wrapped so that current context is propagated to it upon invocation.

Type Parameters

TResult

Type 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 function is 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

function Func<TResult>

Function to which the current context is to be propagated.

Returns

Func<TResult>

Returns function wrapped so that current context is propagated to it upon invocation.

Type Parameters

TResult

Type 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 function is 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).