How to use Sftp events

0 votes
asked May 22 by mwgevans (180 points)

I am creating a .Net Standard class library and wanted to use the CommandSent event, but VS doesn't like

sFTP.CommandSent += new SftpCommadSentEventHandler(CommandSent);

which I copied from your sample application

The following is OK in the compiler but fails at runtime with System.MissingMethodException: Method not found: 'Void Rebex.Net.Sftp.add_CommandSent(System.EventHandler1<Rebex.Net.SftpCommandSentEventArgs>)'.Method not found

sFTP.CommandSent += CommandSent;

Applies to: Rebex SFTP

1 Answer

+1 vote
answered May 22 by Lukas Pokorny (92,430 points)
selected May 22 by mwgevans
 
Best answer

VS doesn't like the first approach because the SftpCommadSentEventHandler delegate is no longer present on modern platforms - it has been replaced with EventHandler<SftpCommandSentEventArgs>. Thanks for bringing this to our attention - we will fix the sample apps as well to use a modern form.

However, your second approach should work.

To see what's going on, I created a simple .NET Standard 2.0 library (which uses Rebex SFTP) and referenced it from a simple .NET Core test app. But this turned out to work fine.

Could you please download my sample library and application from https://www.rebex.net/getfile/37e3963a0e4744cab73142bb5dbaf4cb/RebexSftpSample-CoreApp1.zip and give it a try? If you notice you did something differently, let us know.

commented May 22 by mwgevans (180 points)
Thanks

My console app is not a core app. Added  .Net Framework 4.6 Console app to your project and got the same error.
commented May 22 by Lukas Pokorny (92,430 points)
Thanks! I managed to reproduce the issue. It's apparently caused by the fact that .NET 4.6 console app gets .NET 4.0 binaries of Rebex.Sftp, while the .NET Standard library is linked against .NET Standard 1.5 binaries of Rebex.Sftp, and those two versions are not compatible (due to the difference in CommandSent, among others).
In an ideal world, this would be simple to solve by making the .NET 4.6 app reference .NET Standard 1.5 binaries of Rebex.Sftp as well. But unfortunately, as of today, Visual Studio does not seem to support this (although third-party NuGet package managers such as Paket might).

In future, this issue will most likely disappear in most use cases because we plan to release a .NET Standard 2.0 binaries and .NET 4.6 binaries with identical APIs soon.

Until then, there are two temporary workarounds:

a) Use multi-targeting in the .NET Standard library. Remove <TargetFramework> line from its project file and add something like <TargetFrameworks>netstandard2.0;net461</TargetFrameworks> instead. See https://www.rebex.net/getfile/f4a9b734408e4e8d8c63a8046784a0a1/RebexSftpSample-CoreApp1b.zip for an example.

b) Create customized versions of Rebex NuGet packages that only contain the .NET Standard binaries of Rebex SFTP. This would prevent Visual Studio from picking the wrong binaries for .NET 4.6. Unfortunately, this requires using a custom NuGet repository instead of NuGet.org. If you would like us to create these packages for you, let me know.
commented May 22 by mwgevans (180 points)
Perfect, I've used solution a) and it works so I'll go with that for now.
...