IOperationContext

Namespace: SnowBank.Threading.Operations · interface

Represents the execution context for an asynchronous operation that can be observed from the outside

Properties

Activity

Activity Activity { get; }

Cancellation

CancellationToken Cancellation { get; }

Cancellation token used to abort this operation

Error

OperationError Error { get; }

If not null, error generated by the operation

Id

string Id { get; }

Globally unique identifier for this operation

Key

string Key { get; }

If not null, used to distinguish multiple concurrent operations of the type Type

Parent

IOperationContext Parent { get; }

If not null, parent operation that started this operation

Scheduler

IOperationScheduler Scheduler { get; }

Scheduler used to orchestrate the execution of this operation

State

OperationState State { get; }

Current state of the operation

Type

string Type { get; }

Operation type (for diagnostic purpose)

Methods

Dispatch

void Dispatch(IEvent evt)

Dispatch an event that happened during the processing of this operation

ExecuteStep

IDisposable ExecuteStep(string id, string label = null)

Execute a sub-step, using the same context as this operation

  • id — Id of the sub-step (for computers)
  • label — Label of the sub-step (for humans)

Returns: Token that must be disposed at the end of the sub-step

Sub-steps should be used to wrap the various parts of an operation, that are not large enough to be split into sub-operations.

They will be exposed to observers who are following the execution.

ExecuteSubOperation

Task<OperationResult<TSubResult>> ExecuteSubOperation<TSubResult>(IOperationContext<TSubResult> operation, Func<IOperationContext<TSubResult>, Task<OperationResult<TSubResult>>> handler)

Execute a sub-operation, in its own context.

  • operation — Context of the sub-operation, created as a child of this operation
  • handler — Function that will be run to execute the sub-operation

Returns: Result of the sub-operation.

Log

void Log(LogLevel level, Exception exception, string message, object[] args = null)

TryGetResult

bool TryGetResult(out object result, out Type type)

Return the result of the operation, if it completed successfully

  • result — Result of the operation
  • type — Type of the result

Returns: Returns true if the operation completed successfully; otherwise, false.