ScopedCollection<T> Class

Namespace
Kampute.HttpClient.Utilities
Assembly
  • Kampute.HttpClient.dll

Definition

Holds items in nested scopes that flow with the asynchronous context of the code that begins them.
public class ScopedCollection<T> : IEnumerable<T>
Inheritance
Implements

Type Parameters

T
The type of the items.

Remarks

A scope begun with BeginScope(IEnumerable<T>) holds its items until it is disposed. The items are visible to the code that began the scope and to the asynchronous operations it starts or awaits, but not to unrelated code running at the same time, because the active scope is kept in an AsyncLocal<T>.

Enumerating the collection yields the items of the outermost scope first, so that a consumer that applies them in order lets inner scopes override outer ones. Traverse(Action<T>) visits the innermost scope first.

Properties

HasActiveScope

Gets a value indicating whether a scope is active in the current asynchronous context.
public bool HasActiveScope { get; }

Property Value

bool
true if an active scope is present; otherwise, false.

Methods

BeginScope(IEnumerable<T>)

Begins a scope with the specified items, nested in the active scope if there is one.
public virtual ScopedCollection<T>.Scope BeginScope(IEnumerable<T> items)

Parameters

items IEnumerable<T>
The items of the new scope.

Returns

ScopedCollection<T>.Scope
The new ScopedCollection<T>.Scope, which ends when it is disposed.

Exceptions

ArgumentNullException
Thrown if items is null.

EndScope(ScopedCollection<T>.Scope)

Ends a scope, making its parent the active scope.
protected virtual void EndScope(ScopedCollection<T>.Scope scope)

Parameters

scope ScopedCollection<T>.Scope
The scope to end.

Exceptions

ArgumentNullException
Thrown if scope is null.

GetEnumerator()

Returns an enumerator over the items of the active scopes, starting with the outermost scope.
public virtual IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>
An enumerator over the items.

Traverse(Action<T>)

Applies an action to the items of the active scopes, starting with the innermost scope.
public virtual void Traverse(Action<T> action)

Parameters

action Action<T>
The action to apply to each item.

Exceptions

ArgumentNullException
Thrown if action is null.

Explicit Interface Implementations

IEnumerable.GetEnumerator()

Returns an enumerator that iterates through the collection.
IEnumerator IEnumerable.GetEnumerator()

Returns

IEnumerator
An enumerator that can be used to iterate through the collection.