OperationContextExtensions

Namespace: SnowBank.Threading.Operations · class

Methods

AddBackgroundOperations

static IServiceCollection AddBackgroundOperations(IServiceCollection services)

Registers the IOperationScheduler and IEventBus services

Cancelled

static OperationResult<TResult> Cancelled<TResult>(IOperationContext<TResult> context)

Marks this context as canceled

  • context — Context that has been canceled

Returns: Failed operation result

Failed

static OperationResult<TResult> Failed<TResult>(IOperationContext<TResult> context, string code, string message, object details = null)

Marks this context as failed

  • context — Context that has failed
  • code — Code that represent the specific error condition (for robots)
  • message — Text that describes the error condition (for humans)
  • details — Additional details that can help diagnose the error. This object MUST be serializable into JSON (or other format)

Returns: Failed operation result

InvalidParameter

static OperationResult<TResult> InvalidParameter<TResult>(IOperationContext<TResult> context, string paramName)

Marks this context as failed due to an invalid parameter or argument

  • context — Context that has failed
  • paramName — Name of the parameter or argument that is invalid

Returns: Failed operation result

LogDebug

static void LogDebug(IOperationContext context, string message)

Logs a message using the Debug log level

static void LogDebug(IOperationContext context, string format, params object[] args)

Logs a message using the Debug log level

LogError

static void LogError(IOperationContext context, string message)

Logs a message using the Error log level

static void LogError(IOperationContext context, string format, params object[] args)

Logs a message using the Error log level

static void LogError(IOperationContext context, Exception exception, string message)

Logs a message using the Error log level

LogInformation

static void LogInformation(IOperationContext context, string message)

Logs a message using the Information log level

static void LogInformation(IOperationContext context, string format, params object[] args)

Logs a message using the Information log level

LogTrace

static void LogTrace(IOperationContext context, string message)

Logs a message using the Trace log level

static void LogTrace(IOperationContext context, string format, params object[] args)

Logs a message using the Trace log level

LogWarning

static void LogWarning(IOperationContext context, string message)

Logs a message using the Warning log level

static void LogWarning(IOperationContext context, string format, params object[] args)

Logs a message using the Warning log level

MissingParameters

static OperationResult<TResult> MissingParameters<TResult>(IOperationContext<TResult> context, string paramName)

Marks this context as failed due to a missing required parameter or argument

  • context — Context that has failed
  • paramName — Name of the parameter or argument that is missing

Returns: Failed operation result

Run

static Task<OperationResult<TResult>> Run<TResult>(IOperationContext parent, string type, string key, Func<IOperationContext<TResult>, Task<OperationResult<TResult>>> handler)

Runs a sub-operation that will be attached to this parent Operation

  • parent — Parent operation of this new sub-operation.
  • type — Type (or category) of this sub-operation.
  • key — Key of the sub-operation in this category, or null for operations that are unique per category.
  • handler — Lambda that will be called to run the sub-operation.

Returns: Result of the sub-operation (success or failure)

static Task<OperationResult<TResult>> Run<TResult>(IOperationScheduler scheduler, string type, string key, Func<IOperationContext<TResult>, Task<OperationResult<TResult>>> handler, CancellationToken ct)

Runs an Operation on this scheduler

  • scheduler — Scheduler that will run the operation
  • type — Type (or category) of this operation.
  • key — Key of the operation in this category, or null for operations that are unique per category.
  • handler — Lambda that will be called to run the operation.
  • ct — Token that can be used to cancel the operation

Returns: Result of the operation (success or failure)

static Task<OperationResult<TResult>> Run<TResult, T0>(IOperationScheduler scheduler, string type, string key, Func<IOperationContext<TResult>, T0, Task<OperationResult<TResult>>> handler, T0 arg0, CancellationToken ct)

Runs an Operation on this scheduler

  • scheduler — Scheduler that will run the operation
  • type — Type (or category) of this operation.
  • key — Key of the operation in this category, or null for operations that are unique per category.
  • handler — Lambda that will be called to run the operation.
  • arg0 — Argument of the operation
  • ct — Token that can be used to cancel the operation

Returns: Result of the operation (success or failure)

static Task<OperationResult<TResult>> Run<TResult, T0, T1>(IOperationScheduler scheduler, string type, string key, Func<IOperationContext<TResult>, T0, T1, Task<OperationResult<TResult>>> handler, T0 arg0, T1 arg1, CancellationToken ct)

Runs an Operation on this scheduler

  • scheduler — Scheduler that will run the operation
  • type — Type (or category) of this operation.
  • key — Key of the operation in this category, or null for operations that are unique per category.
  • handler — Lambda that will be called to run the operation.
  • arg0 — First argument of the operation
  • arg1 — Second argument of the operation
  • ct — Token that can be used to cancel the operation

Returns: Result of the operation (success or failure)

static Task<OperationResult<TResult>> Run<TResult, T0, T1, T2>(IOperationScheduler scheduler, string type, string key, Func<IOperationContext<TResult>, T0, T1, T2, Task<OperationResult<TResult>>> handler, T0 arg0, T1 arg1, T2 arg2, CancellationToken ct)

Runs an Operation on this scheduler

  • scheduler — Scheduler that will run the operation
  • type — Type (or category) of this operation.
  • key — Key of the operation in this category, or null for operations that are unique per category.
  • handler — Lambda that will be called to run the operation.
  • arg0 — First argument of the operation
  • arg1 — Second argument of the operation
  • arg2 — Second argument of the operation
  • ct — Token that can be used to cancel the operation

Returns: Result of the operation (success or failure)

Throw

static OperationResult<TResult> Throw<TResult>(IOperationContext<TResult> context, Exception error, string code = null, string message = null, object details = null)

Marks this context as failed due to an internal exception

  • context — Context that has failed
  • error — Exception that was caught (or manually thrown) during the execution of this operation
  • code — Optional code that represent the specific error condition (for robots)
  • message — Optional text that describes the error condition (for humans)
  • details — Optional additional details that can help diagnose the error. This object MUST be serializable into JSON (or other format)

Returns: Failed operation result

ThrowIfCancellationRequested

static void ThrowIfCancellationRequested(IOperationContext context)

Throws a OperationCanceledException if this context has been aborted