Table of Contents

Class AsyncDisposableExtensions

Namespace
VpnHood.Net.Toolkit.Extensions
Assembly
VpnHood.Net.Toolkit.dll

Provides disposal helpers for IAsyncDisposable that log a failure instead of throwing it.

public static class AsyncDisposableExtensions
Inheritance
AsyncDisposableExtensions
Inherited Members

Methods

TryDisposeAsync(IAsyncDisposable?, LogLevel)

Disposes an object asynchronously, and logs a failure instead of throwing it; a null object is passed over.

public static ValueTask TryDisposeAsync(this IAsyncDisposable? disposable, LogLevel failureLevel = LogLevel.Debug)

Parameters

disposable IAsyncDisposable

The object, or null.

failureLevel LogLevel

The level a failure is logged at; a cancellation is logged at Debug whatever the level. The default, Debug, suits the bulk network closes, whose peer is often gone already; a close that happens once per process passes Warning.

Returns

ValueTask

A task that completes when the close does, failed or not.

Remarks

Only the asynchronous close runs, even after it fails: a synchronous one may block.

TryDisposeAsync(IAsyncDisposable?, TimeSpan, LogLevel)

Disposes an object asynchronously, waiting for it at most a timeout, and logs a failure instead of throwing it; a null object is passed over.

public static ValueTask TryDisposeAsync(this IAsyncDisposable? disposable, TimeSpan timeout, LogLevel failureLevel = LogLevel.Debug)

Parameters

disposable IAsyncDisposable

The object, or null.

timeout TimeSpan

How long to wait for the close.

failureLevel LogLevel

The level a failure is logged at; a cancellation is logged at Debug.

Returns

ValueTask

A task that completes when the close does, failed or not, or at the timeout.

Remarks

Past the timeout, a warning is logged and the close goes on without its caller; a failure it ends with is still logged. The wait starts once DisposeAsync() returns its task, so work it does before that is not bounded.

Exceptions

ArgumentOutOfRangeException

timeout is negative, other than InfiniteTimeSpan, or too long.