Skip to content

About

IDisposable and IAsyncDisposable building blocks for .NET: delegate-based disposables, reverse-order collections, swappable and single-assignment holders, reference-counted sharing, lock scopes and cancel-on-dispose.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

494 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

NCode Group

NCode.Disposables

Building blocks for IDisposable and IAsyncDisposable in .NET: run an action on dispose, dispose many resources in reverse order, hold a swappable, serial or single-assignment resource, guard a resource until ownership moves on, cancel on dispose, share a resource through reference-counted leases, scope a lock, and dispose on a synchronization context.

NuGet version NuGet downloads CI Target framework Documentation License OpenSSF Scorecard OpenSSF Best Practices

Cleanup in .NET often needs more than one using statement: undoing a registration, disposing several resources in the right order, handing one expensive resource to several owners, or disposing a UI object on its own thread. NCode.Disposables gives each of these a small type behind the Disposable and AsyncDisposable factories, each with an IAsyncDisposable counterpart except the flag and the lock scopes.

This page is the full guide; nuget.org shows the shorter package readme.

The documentation site has a quick start, a picker that finds the type for your situation, a guide for each task, the rules behind them, a behavior reference of every disposable and an API map of every public member.

Issues and discussion are welcome, and code is by invitation while the design settles, as CONTRIBUTING.md says.

Installation

dotnet add package NCode.Disposables

The package depends on no other package. Its target frameworks are the ones its .csproj declares; they are listed on nuget.org's Frameworks tab and shown by the badge above. CHANGELOG.md records when a framework is added or dropped.

Quick start

Every type is in the NCode.Disposables namespace. Disposable.Create turns a delegate into an IDisposable, and Disposable.Collection disposes many of them together, the last one added first:

var log = new List<string>();
var connection = Disposable.Create(() => log.Add("connection closed"));
var fileLock = Disposable.Create(() => log.Add("lock released"));

using (var resources = Disposable.Collection())
{
    resources.Add(connection);
    resources.Add(fileLock);
}

// log: "lock released", "connection closed": the last resource added is disposed first

Usage

Running an action on dispose

Disposable.Create wraps an Action. By default the action runs on the first Dispose only, even when several threads dispose at once; Disposable.CreateRepeatable runs it on every call:

var count = 0;

var once = Disposable.Create(() => count++);
once.Dispose();
once.Dispose(); // count == 1: the action runs on the first call only

var everyTime = Disposable.CreateRepeatable(() => count++);
everyTime.Dispose();
everyTime.Dispose(); // count == 3: the action runs on every call

Overloads take up to eight state arguments, which the action receives when it runs, and the Func overloads discard the delegate's result:

var stopwatch = Stopwatch.StartNew();

using (Disposable.Create(watch => watch.Stop(), stopwatch))
{
    // timed work
}

// stopwatch.IsRunning == false

AsyncDisposable.Create takes a Func<ValueTask>, or an Action that it runs synchronously, and works with await using:

var stream = new MemoryStream();
var writer = new StreamWriter(stream);

await using (AsyncDisposable.Create(async () => await writer.FlushAsync()))
{
    await writer.WriteAsync("hello");
}

// stream.Length == 5: the writer flushed when the scope ended

AsyncDisposable.Create also passes up to eight arguments to a Func<…, ValueTask>, so a static lambda needs no closure:

var stream = new MemoryStream();
var writer = new StreamWriter(stream);

await using (AsyncDisposable.Create(static w => new ValueTask(w.FlushAsync()), writer))
{
    await writer.WriteAsync("hello");
}

// stream.Length == 5

Running an action without allocating

Disposable.CreateValue and AsyncDisposable.CreateValue return a struct that runs a static delegate with a state value. It allocates nothing, but every dispose of every copy runs the action again, so dispose it once, through using:

var stopwatch = Stopwatch.StartNew();

using (Disposable.CreateValue(static watch => watch.Stop(), stopwatch))
{
    // timed work; the struct allocates nothing
}

// stopwatch.IsRunning == false

The empty disposable

Disposable.Empty and AsyncDisposable.Empty are singletons whose dispose does nothing, for a method that must return a disposable when there is nothing to clean up:

static IDisposable Subscribe(bool enabled) =>
    enabled ? Disposable.Create(() => Console.WriteLine("Unsubscribed")) : Disposable.Empty;

using var subscription = Subscribe(enabled: false); // Disposable.Empty: disposing it does nothing

Swapping the resource an aggregate holds

Disposable.Aggregate holds one resource, or null, in its Disposable property, and disposes the resource it holds when it is disposed. Setting the property replaces the resource without disposing the old one. Once the aggregate is disposed, the property reads null and setting it disposes the resource it is given; AsyncDisposable.Aggregate's setter throws ObjectDisposedException then, and its SetDisposableAsync disposes the resource. AsyncDisposable.Aggregate does the same for an IAsyncDisposable:

var log = new List<string>();
var primary = Disposable.Create(() => log.Add("primary closed"));
var replica = Disposable.Create(() => log.Add("replica closed"));

using (var current = Disposable.Aggregate(primary))
{
    current.Disposable = replica; // swapping doesn't dispose primary
    primary.Dispose();
}

// log: "primary closed", "replica closed"

Replacing a resource and disposing the old one

Disposable.Serial holds one resource and disposes it when another replaces it. Once the serial disposable is disposed, it disposes any resource it is given, and Disposable reads null. AsyncDisposable.Serial does the same through SetDisposableAsync:

var log = new List<string>();
using (var subscription = Disposable.Serial())
{
    subscription.Disposable = Disposable.Create(() => log.Add("first closed"));
    subscription.Disposable = Disposable.Create(() => log.Add("second closed")); // disposes the first
}

// log: "first closed", "second closed"

Assigning a resource once

Disposable.SingleAssignment takes one resource, once; a second assignment throws InvalidOperationException. A resource that arrives after it is disposed is disposed at once. AsyncDisposable.SingleAssignment takes it through SetDisposableAsync:

var log = new List<string>();
var pending = Disposable.SingleAssignment();
pending.Dispose(); // disposed before the resource arrived
pending.Disposable = Disposable.Create(() => log.Add("late resource closed")); // disposed at once

// log: "late resource closed"

Disposing many resources together

Disposable.Collection is an ICollection<IDisposable> that disposes its items in reverse order, the last one added first, on its first Dispose; later calls do nothing, and an item added after that is disposed at once. Remove and Clear take items out without disposing them:

var log = new List<string>();
var first = Disposable.Create(() => log.Add("first"));
var second = Disposable.Create(() => log.Add("second"));
var third = Disposable.Create(() => log.Add("third"));

using (var resources = Disposable.Collection(first, second, third))
{
    resources.Remove(second); // removing an item doesn't dispose it
}

// log: "third", "first"

Disposable.Collection's Add disposes an item added after the collection is disposed, instead of keeping it:

var log = new List<string>();
var resources = Disposable.Collection();
resources.Dispose();
var lateItem = Disposable.Create(() => log.Add("late item closed"));

resources.Add(lateItem); // disposed at once, not kept

// log: "late item closed"; resources.Count == 0

AsyncDisposable.Collection's Add throws ObjectDisposedException after the collection is disposed, because it can't await the item's disposal; AddAsync disposes the item instead:

var log = new List<string>();
var resources = AsyncDisposable.Collection();
await resources.DisposeAsync();
var lateItem = AsyncDisposable.Create(() => log.Add("late item closed"));

await resources.AddAsync(lateItem); // disposed at once, not kept; Add would throw ObjectDisposedException

// log: "late item closed"; resources.Count == 0

Every item is disposed even when one throws. A single failure is rethrown as it is, several are thrown together as one AggregateException, and Disposable.CollectionIgnoringExceptions ignores them:

var log = new List<string>();
var first = Disposable.Create(() => log.Add("first"));
var failing = Disposable.Create(() =>
    throw new InvalidOperationException("Failed to close.")
);
var third = Disposable.Create(() => log.Add("third"));
var resources = Disposable.CollectionIgnoringExceptions(first, failing, third);

resources.Dispose(); // log: "third", "first": the failure is ignored

An item that also implements IAsyncDisposable, such as a Stream, is disposed through IDisposable.Dispose, like every other item:

// stream is an open Stream, such as a FileStream.
using (var resources = Disposable.Collection(stream))
{
    stream.WriteByte(42);
}

// stream.Dispose ran: the collection disposes every item through IDisposable

To dispose such items with DisposeAsync, put them in AsyncDisposable.Collection.

Sharing a resource through leases

SharedReference.Create puts a resource behind a reference count and returns the first lease. Each AddReference returns another lease on the same resource, and the resource is disposed when the last lease is disposed:

var log = new List<string>();
var connection = Disposable.Create(() => log.Add("connection closed"));

var first = SharedReference.Create(connection);
var second = first.AddReference();

first.Dispose(); // log is empty: second still holds a reference
second.Dispose(); // log: "connection closed"

Disposable.Shared(value) and the AsSharedReference() extension do the same. Once the last lease is released, AddReference and Value throw ObjectDisposedException, and TryAddReference returns false:

var resource = Disposable.Create(() => { });
var lease = SharedReference.Create(resource);
lease.Dispose(); // the last lease is released, so the resource is disposed

var added = lease.TryAddReference(out var extra); // false: no lease can be added

A lease is a readonly struct, so a copy of a lease is the same lease: dispose each lease exactly once, because disposing it twice releases two references. IsActive is false only for a default lease, whose Value and AddReference throw InvalidOperationException.

For a value that isn't disposable, or that needs other cleanup, pass the release action yourself:

var pool = ArrayPool<byte>.Create();
var buffer = pool.Rent(1024);

using (var lease = SharedReference.Create(buffer, array => pool.Return(array)))
{
    lease.Value[0] = 42;
}

// the buffer is back in the pool

AsyncSharedReference.Create does the same for an IAsyncDisposable, or with a Func<T, ValueTask> release action, and returns its first lease synchronously:

// stream is an open Stream, such as a FileStream.
await using (var lease = AsyncSharedReference.Create(stream))
{
    lease.Value.WriteByte(42);
}

// stream.DisposeAsync ran when the last lease was released

The AsAsyncSharedReference() extension does the same, also for a type that implements both IDisposable and IAsyncDisposable, such as a Stream:

// stream is an open Stream, which implements both IDisposable and IAsyncDisposable.
await using (var lease = stream.AsAsyncSharedReference())
{
    lease.Value.WriteByte(42);
}

// stream.DisposeAsync ran when the last lease was released

On a type that implements both IDisposable and IAsyncDisposable, such as a MemoryStream, AsSharedReference() returns a SharedReferenceLease<T>, which disposes the resource with Dispose, and AsAsyncSharedReference() returns an AsyncSharedReferenceLease<T>, which disposes it with DisposeAsync.

Handing on ownership with a guard

Disposable.Guard disposes a resource when the scope ends, unless Release() hands it on first, so a factory can't leak what it built when a later step throws. AsyncDisposable.Guard does the same for an IAsyncDisposable:

var log = new List<string>();

DisposableAction Connect(bool handshakeFails)
{
    var connection = Disposable.Create(() => log.Add("connection closed"));
    using var guard = Disposable.Guard(connection);

    if (handshakeFails)
        throw new InvalidOperationException("Handshake failed."); // the guard closes the connection

    return guard.Release(); // the caller owns the connection now
}

using (Connect(handshakeFails: false)) { }

try
{
    Connect(handshakeFails: true);
}
catch (InvalidOperationException)
{
    log.Add("handshake failed");
}

// log: "connection closed", "connection closed", "handshake failed"

Cancelling on dispose

Disposable.Cancellation() cancels its token when disposed, then disposes the CancellationTokenSource it created; Disposable.Cancellation(source) only cancels a source you own. AsyncDisposable.Cancellation cancels with CancelAsync:

using var cancellation = Disposable.Cancellation();
var work = Task.Delay(Timeout.Infinite, cancellation.Token);
cancellation.Dispose(); // cancels the token, then disposes the source it created
// work.IsCanceled == true

Checking whether a disposable is disposed

Every disposable class that keeps disposal state implements IDisposalStatus, whose IsDisposed is true once disposal has started. The structs, which are the value disposables, the lock scopes and the shared-reference leases, keep no disposal state and don't implement it, and a shared-reference owner is a reference count, not a disposable. Disposable.Flag() only records its disposal:

var flag = Disposable.Flag();
var cleanup = Disposable.Create(() => { });
flag.Dispose();
IDisposalStatus status = cleanup; // every disposable that keeps state reports it
// flag.IsDisposed == true, status.IsDisposed == false

Disposing on a synchronization context

Disposable.Context and AsyncDisposable.Context dispose a resource on a SynchronizationContext, such as a UI thread's. By default Dispose calls the context's Send and returns once the resource is disposed; Disposable.PostingContext calls Post instead and returns at once, telling the context through OperationStarted and OperationCompleted. The resource is disposed once, however often Dispose is called:

var log = new List<string>();
var control = Disposable.Create(() => log.Add("control disposed"));

// uiContext is the SynchronizationContext captured on the UI thread.
var disposable = Disposable.Context(control, uiContext);
disposable.Dispose(); // Send: returns once the UI thread has disposed the control

AsyncDisposable.Context posts the disposal to the context and completes once it has run there:

var log = new List<string>();
var viewModel = AsyncDisposable.Create(() => log.Add("view model disposed"));

// uiContext is the SynchronizationContext captured on the UI thread.
var disposable = AsyncDisposable.Context(viewModel, uiContext);
await disposable.DisposeAsync(); // Post: completes once the UI thread has disposed it

When the context's Post throws, Dispose on a PostingContext and DisposeAsync call OperationCompleted and throw that exception. It doesn't dispose the resource on the calling thread, because a resource such as a UI control must be disposed on its context.

Scoping a lock

semaphore.Lock() and LockAsync() wait for a SemaphoreSlim and return a scope that releases it; ReadLock(), UpgradeableReadLock() and WriteLock() do the same for a ReaderWriterLockSlim. A scope is a struct that allocates nothing: dispose it once, through using:

using var semaphore = new SemaphoreSlim(1, 1);
var countInScope = -1;
using (await semaphore.LockAsync())
{
    countInScope = semaphore.CurrentCount; // 0 while the scope holds it
}
// semaphore.CurrentCount == 1
using var cacheLock = new ReaderWriterLockSlim();
var readLockHeld = false;
using (cacheLock.ReadLock())
{
    readLockHeld = cacheLock.IsReadLockHeld; // true while the scope holds it
}
using (cacheLock.WriteLock())
{
    // update the cache
}
// cacheLock.IsWriteLockHeld == false

Bridging sync and async disposal

AsyncDisposable.Adapt wraps an IDisposable as an IAsyncDisposable, for code that only takes the async form. It disposes the resource once; AsyncDisposable.AdaptRepeatable disposes it on every call:

var log = new List<string>();
IDisposable legacy = Disposable.Create(() => log.Add("legacy disposed"));

await using (AsyncDisposable.Adapt(legacy))
{
    // code that takes an IAsyncDisposable
}

// log: "legacy disposed"

DisposeAsyncIfAvailable() calls DisposeAsync when the instance implements IAsyncDisposable, Dispose otherwise, and does nothing for null:

// stream is an open Stream, such as a FileStream.
IDisposable? resource = stream;
await resource.DisposeAsyncIfAvailable(); // a Stream is IAsyncDisposable: DisposeAsync runs

resource = null;
await resource.DisposeAsyncIfAvailable(); // null: nothing happens

Disposing every item in a list

DisposeAll() disposes each IDisposable in an IEnumerable<object?> in reverse order, also one that implements IAsyncDisposable, such as a Stream, and skips null and every other item. Its errors work as a collection's do, and DisposeAllIgnoringExceptions() ignores them. An item that implements only IAsyncDisposable isn't disposed: once the other items are disposed, DisposeAll() throws InvalidOperationException for it, which DisposeAllIgnoringExceptions() doesn't ignore:

var log = new List<string>();
object?[] items =
[
    Disposable.Create(() => log.Add("first")),
    "not disposable",
    null,
    Disposable.Create(() => log.Add("second")),
];

items.DisposeAll(); // log: "second", "first": items that aren't IDisposable are skipped

DisposeAllAsync() calls DisposeAsync on an item that implements IAsyncDisposable and Dispose on any other IDisposable, also in reverse order:

var log = new List<string>();
// stream is an open Stream, such as a FileStream.
object[] items = [Disposable.Create(() => log.Add("disposed")), stream];

await items.DisposeAllAsync(); // DisposeAsync for the stream, then Dispose for the rest

API overview

Factories

Type Members
Disposable Empty, Create, CreateRepeatable, CreateValue, Aggregate, Serial, SingleAssignment, Guard, Cancellation, Flag, Collection, CollectionIgnoringExceptions, Context, PostingContext and Shared
AsyncDisposable Empty, Create, CreateRepeatable, CreateValue, Adapt, AdaptRepeatable, Aggregate, Serial, SingleAssignment, Guard, Cancellation, Collection, CollectionIgnoringExceptions, Context and Shared
SharedReference Create(value) for an IDisposable, and Create(value, onRelease) for any value
AsyncSharedReference Create(value) for an IAsyncDisposable, and Create(value, onRelease) for any value

Types

Type Role
DisposableEmpty, AsyncDisposableEmpty Do nothing; Singleton is the instance Empty returns
DisposableAction, AsyncDisposableAction Run a delegate on dispose, once, or on every dispose from CreateRepeatable
DisposableAggregate, AsyncDisposableAggregate Hold one replaceable resource
DisposableSerial, AsyncDisposableSerial Hold one resource and dispose the previous one when it is replaced
DisposableSingleAssignment, AsyncDisposableSingleAssignment Take one resource, once
DisposableGuard<T>, AsyncDisposableGuard<T> Dispose a resource unless Release() hands ownership on
DisposableCancellation, AsyncDisposableCancellation Cancel a token on dispose
DisposableFlag Only records that it was disposed
DisposableCollection, AsyncDisposableCollection Dispose their items in reverse order
DisposableContext Disposes a resource on a SynchronizationContext
AsyncDisposableContext Posts the async disposal of a resource to a SynchronizationContext
ValueDisposable<TState>, AsyncValueDisposable<TState> An allocation-free struct that runs a static delegate with its state on every dispose
IDisposalStatus Exposes IsDisposed
AsyncDisposableAdapter An IAsyncDisposable over an IDisposable
SharedReferenceLease<T>, AsyncSharedReferenceLease<T> A lease on a shared resource: Value, IsActive, AddReference and TryAddReference
SharedReferenceOwner<T>, AsyncSharedReferenceOwner<T> The reference count behind the leases; ISharedReferenceOwner<T> and IAsyncSharedReferenceOwner<T>

Extensions

Type Members
DisposableExtensions DisposeAsyncIfAvailable(), AsSharedReference(), DisposeAll() and DisposeAllIgnoringExceptions()
AsyncDisposableExtensions AsAsyncSharedReference(), DisposeAllAsync() and DisposeAllIgnoringExceptionsAsync()
SemaphoreSlimExtensions Lock() and LockAsync()
ReaderWriterLockSlimExtensions ReadLock(), UpgradeableReadLock() and WriteLock()

Thread safety

Every class runs its cleanup at most once, even when several threads dispose it at once, except an action or adapter from CreateRepeatable or AdaptRepeatable. The aggregate, serial and single-assignment holders swap their resource atomically, and a collection's members all take one lock, its enumeration walking a snapshot. A resource handed to a holder after it is disposed is disposed at once. A lease's AddReference, TryAddReference and Dispose are safe from any thread, and the release action runs once, on the thread that releases the last lease. The structs (ValueDisposable<TState>, AsyncValueDisposable<TState> and the lock scopes) run their action on every dispose: dispose them once.

Project

About

IDisposable and IAsyncDisposable building blocks for .NET: delegate-based disposables, reverse-order collections, swappable and single-assignment holders, reference-counted sharing, lock scopes and cancel-on-dispose.

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages