0

I'm trying to upload a file using SSH.NET using C# in a .NET Framework 4.5.2 project.

As soon as the ConnectionInfo constructor is invoked the application exits without any Exception not error message. The event viewer is clean.

It seems I'm running into an AccessViolationException, that I know it's a serious problem and cannot be caught inside managed code but... What am I doing wrong in using this library? I just need to upload my file to a SFTP server.

Here is my code:

public override void Upload(FtpConfiguration conf, string file)
{
    var fi = new FileInfo(file);

    try
    {
        // As soon as code reach this line the application crashes
        var connectionInfo = new ConnectionInfo(conf.Server,
                                                conf.Username,
                                                new PasswordAuthenticationMethod(conf.Username, conf.Password)
                                                );
        using (var sftp = new SftpClient(connectionInfo))
        {
            sftp.Connect();

            if (sftp.IsConnected)
            {
                if (!string.IsNullOrEmpty(conf.Subfolder))
                    sftp.ChangeDirectory(conf.Subfolder);
                using (var fileStream = new FileStream(fi.FullName, FileMode.Open))
                {
                    sftp.UploadFile(fileStream, fi.Name, null);
                }
                sftp.Disconnect();
            }
        }
    }
    catch (Exception ex)
    {
        // Just for debug purpose
        var error = ex.Message;

        throw ex;
    }
}
Nate W
  • 275
  • 2
  • 13
WaveMax
  • 191
  • 4
  • 15
  • I already tried to decorate the method with `HandleProcessCorruptedStateExceptions` but still failed to catch the exception – WaveMax Mar 09 '21 at 14:07
  • Did you follow this guide: https://stackoverflow.com/a/4759831/1220550 (no experience with it, I just found it) – Peter B Mar 09 '21 at 14:09
  • have you tried this https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/legacycorruptedstateexceptionspolicy-element ? – NullReference Mar 09 '21 at 14:09
  • Yes, already followed both links with no luck so far – WaveMax Mar 09 '21 at 14:10

2 Answers2

0

Well, I suppose that all your FtpConfiguration conf properties used in both ConnectionInfo and PasswordAuthenticationMethod are all of type string, but I think you're missing the port which must be int. This should work.

ConnectionInfo connectionInfo = new ConnectionInfo(host, port, user,
                                     new PasswordAuthenticationMethod(user, pass));

using (var client = new SftpClient(connectionInfo))
{

      client.Connect();
      // Do some stuff.
      client.Disconnect();
}
ɐsɹǝʌ ǝɔıʌ
  • 4,440
  • 3
  • 35
  • 56
  • I just added the port (default 22) but the behaviour seems to be the same – WaveMax Mar 09 '21 at 14:37
  • Did you also check the values for `host`, `user` and `pass` when creating the `ConnectionInfo` object? – ɐsɹǝʌ ǝɔıʌ Mar 09 '21 at 14:44
  • Yes, all the info are correct. FileZilla works fine with the same values – WaveMax Mar 09 '21 at 14:50
  • I tried both prepending "sftp://" and using the host by itself, but with the same result. But if it was a credential or host problem I would not expect such a serious issue... a normal exception would have been enough – WaveMax Mar 09 '21 at 14:57
  • I'm trying to figure out what's the issue as your code seems right to me and mine was successfully tested. `host` must not contain the protocol, so some like "ftp.myhost.com" is correct. – ɐsɹǝʌ ǝɔıʌ Mar 09 '21 at 15:06
0

I think I found a solution. My Upload method was inside a Task async chain. Getting rid of the async solved the problem.

WaveMax
  • 191
  • 4
  • 15