We have been postponing CancellationToken
support for mainly two reasons:
We still plan to support legacy platforms such as .NET Framework 2.0/3.5 or .NET Compact Framework 3.5/3.9 in the forthcoming version 8.0. So at this point, upgrading large parts of our codebase in order to add CancellationToken
would be impractical. We will revisit this for version 9.0.
We plan to introduce a new fully-asynchronous SSH and SFTP core in version 9.0, which will greatly facilitate the addition of CancellationToken
support.
We feel that the current cancellation model is flawed. Having to manually add CancellationToken
argument to any async method that performs any cancelable operation seems somewhat bloated and unnecessarily complicated. We have been hoping that Microsoft adds some new C# syntax to make this streamlined, but there is still no sing of this.
Adding CancellationToken
to Rebex SFTP might introduce a false sense of safety. Most SFTP operations are not actually cancelable, and it's not even clear what a request to cancel them is supposed to mean. A distinction between graceful and ungraceful cancelation would be very helpful indeed. Again, we have been hoping for some improvements in .NET in this area, but to no avail.
Still, we will apparently have to add CancellationToken
support soon, but figuring workaround for the problems outlined above is going to be complicated. We will most likely end up with a model resembling the behavior of the current Sftp.Timeout
, where canceling the token would trigger a similar mechanism.
However, in the meantime, using AbortTransfer
and/or Dispose
is the way to go. AbortTransfer
only affects file transfers, listings, and multi-file methods, so a perhaps it would be best to call AbortTransfer(null)
first to abort these, followed by Dispose()
in case the active operations don't end within a second or so.
In most scenarios, I would not recommend implementing a wrapper around Sftp
with all methods. I would rather create a wrapper that only exposes the SFTP functionality you need in your project, which might not necessarily have to mirror the Rebex API. If you are only going to "place a file containing the message on an SFTP server", then perhaps you would be fine with a single method that accepts all the parameters it needs (server name, fingerprint, credentials, source path and target path) along with CancellationToken
, and perform cleanup using AbortTransfer
/Dispose
internally if it gets canceled. One we add cancellation support, you would just refactor the internals of this and keep the interfacing to the rest of your app intact.
The good news is that Rebex SFTP library is going to be the first one to get cancellation support, so hopefully the wait is not going to be too long.