Table of Contents

Method WatchAsync

Namespace
SshSharp.Extensions.DependencyInjection
Assembly
SshSharp.Extensions.DependencyInjection.dll

WatchAsync(string, CancellationToken)

The changes of the shared connection for name, as they happen, until cancellationToken is cancelled or the provider is disposed: Connected when it is opened, Disconnected when it ends, and Reconnected when a new connection replaces it. Use it to restore what lived on the old connection (forwardings, long-running commands) on the new one. Each enumeration is a watcher of its own; none misses an event, however slowly it reads.

IAsyncEnumerable<SshConnectionEvent> WatchAsync(string name, CancellationToken cancellationToken = default)

Parameters

name string

The endpoint, as registered with AddSshClient.

cancellationToken CancellationToken

Stops watching; the enumeration then ends with OperationCanceledException.

Returns

IAsyncEnumerable<SshConnectionEvent>

Remarks

A watcher first gets the current state, when there is one: Connected when the connection is up, Disconnected when the last one ended and none has replaced it yet. So connecting first and then watching misses nothing.

Watching does not connect or reconnect: the provider connects when GetClientAsync(string, CancellationToken) is called. A watcher that should keep the connection up calls GetClientAsync(string, CancellationToken) after Disconnected, which waits between failed attempts as described there. Watching starts when the enumeration does (the first MoveNextAsync), and ends with it (cancelling, or leaving await foreach).

Exceptions

InvalidOperationException

No endpoint is registered as name.

NotSupportedException

The implementation does not support watching (the default for implementations other than the package's own, such as test fakes written before it existed).