Execution Context

While executing pipelines and modules, the current state and other functionality is passed in an object called the execution context.

The execution context contains lots of information such as the current pipeline, phase, and module, the settings and file system, the input documents to the module, and more. The current execution context is generally provided by configuration delegates or a module execution method.

Interfaces

The execution context implements a number of interfaces that provide additional capabilities:

  • IMetadata: All settings can be accessed directly through the execution context as metadata.
  • ILogger: All logging should be performed through the execution context, which tracks the current pipeline and module and logs them out along with your messages.
  • IDocumentFactory: The execution context can be used to create new documents.
  • IServiceProvider: allows you to use the execution context as a dependency injection service provider.

Properties

In addition to directly implementing these interfaces, the execution context also contains a number of useful properties:

  • ExecutionId: Uniquely identifies the current execution cycle which can be helpful for managing caches or resetting data.
  • CancellationToken: Should be used whenever by possible by modules and other code to provide cancellation support.
  • ApplicationState: Contains initial application state data like the original command line arguments.
  • ClassCatalog: A catalog of all classes in all referenced assemblies.
  • Events: The collection of registered events.
  • FileSystem: The current file system abstraction.
  • Settings: The application settings as metadata (even though this is also exposed directly by the execution context by implementing IMetadata).
  • Shortcodes: A read-only collection of all available shortcodes.
  • Namespaces: Gets a set of namespaces that should be brought into scope for modules that perform dynamic compilation.
  • MemoryStreamFactory: Provides pooled memory streams (via the RecyclableMemoryStream library).
  • Outputs: Gets a collection of outputs from all previously processed pipelines.
  • OutputPages: Gets a collection of outputs from all previously processed pipelines, filtered to just those documents with a destination path ending in one of the configured page extensions (such as ".html" or ".htm").
  • Services: Gets the dependency injection service provider (even though this is also exposed directly by the execution context by implementing IServiceProvider).
  • ScriptHelper: Gets a helper that can compile and evaluate C# scripts.
  • Pipelines: Gets a read-only collection of all defined pipelines.
  • PipelineName: Gets the name of the currently executing pipeline.
  • Pipeline: Gets the currently executing pipeline.
  • Phase: Gets the currently executing phase of the current pipeline.
  • Parent: Gets the parent execution context if currently in a nested module.
  • Module: Gets the current executing module.
  • Inputs: The collection of input documents to the current module (probably the most important property of the execution context).

Current Execution Context

You can get the current execution context at any time by calling IExecutionContext.Current. This static default interface property is implemented using an AsyncLocal<IExecutionContext> so it always contains the correct execution context even across asynchronous calls.