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
disposableIAsyncDisposableThe object, or null.
failureLevelLogLevelThe 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
disposableIAsyncDisposableThe object, or null.
timeoutTimeSpanHow long to wait for the close.
failureLevelLogLevelThe 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
timeoutis negative, other than InfiniteTimeSpan, or too long.