Class ScopeContext
Represents the context for managing database and operational scopes within the Umbraco CMS infrastructure. Provides access to the current active scope and the scope provider, enabling coordinated resource management and transactional operations. Typically used internally to ensure correct scope handling during repository and service operations.
Inheritance
Namespace: Umbraco.Cms.Core.Scoping
Assembly: Umbraco.Infrastructure.dll
Syntax
public class ScopeContext : IScopeContext, IInstanceIdentifiable
Constructors
View SourceScopeContext()
Declaration
public ScopeContext()
Properties
View SourceCreatedThreadId
Gets the ID of the thread that created this scope context.
Declaration
public int CreatedThreadId { get; }
Property Value
| Type | Description |
|---|---|
| int |
InstanceId
Gets the unique identifier for this instance of the scope context.
Declaration
public Guid InstanceId { get; }
Property Value
| Type | Description |
|---|---|
| Guid |
Methods
View SourceEnlist(string, Action<bool>, int)
Enlists an action to be executed when the scope completes, with an optional priority.
Declaration
public void Enlist(string key, Action<bool> action, int priority = 100)
Parameters
| Type | Name | Description |
|---|---|---|
| string | key | A unique key identifying this enlistment, used to prevent duplicate actions. |
| Action<bool> | action | The action to execute. The boolean parameter indicates whether the scope completed successfully ( |
| int | priority | The priority of the action; lower values indicate higher priority. Default is 100. |
Enlist<T>(string, Func<T>?, Action<bool, T?>?, int)
Enlists an item in the scope context with the specified key, optionally creating it if it does not exist, and allows an action to be performed when the scope completes.
Declaration
public T? Enlist<T>(string key, Func<T>? creator, Action<bool, T?>? action = null, int priority = 100)
Parameters
| Type | Name | Description |
|---|---|---|
| string | key | A unique key used to identify the enlisted item within the scope context. |
| Func<T> | creator | A function to create the item if it does not already exist in the scope; if null and the item does not exist, no item is created. |
| Action<bool, T> | action | An optional action to invoke when the scope completes. The boolean parameter indicates whether the scope completed successfully, and the second parameter is the enlisted item. |
| int | priority | The priority for the enlisted item, which determines the order in which actions are executed when the scope completes. Lower values indicate higher priority. |
Returns
| Type | Description |
|---|---|
| T | The enlisted item of type |
Type Parameters
| Name | Description |
|---|---|
| T |
GetEnlisted<T>(string)
Retrieves an enlisted object of the specified type associated with the given key.
Declaration
public T? GetEnlisted<T>(string key)
Parameters
| Type | Name | Description |
|---|---|---|
| string | key | The key identifying the enlisted object. |
Returns
| Type | Description |
|---|---|
| T | The enlisted object of type |
Type Parameters
| Name | Description |
|---|---|
| T | The type of the enlisted object to retrieve. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if an object with the specified key exists but is not of type |
ScopeExit(bool)
Executes all enlisted actions in order of priority when the scope exits. This method is typically called at the end of a scope to ensure that all registered (enlisted) actions are executed, passing the completion status to each. If any enlisted action throws an exception, all exceptions are aggregated and thrown after all actions have run.
Declaration
public void ScopeExit(bool completed)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | completed | True if the scope completed successfully; otherwise, false. |