简体   繁体   English

SSH.NET 无法捕获的异常

[英]SSH.NET uncatchable exception

I'm trying to upload a file using SSH.NET using C# in a .NET Framework 4.5.2 project.我正在尝试在 .NET Framework 4.5.2 项目中使用 C# 使用 SSH.NET 上传文件。

As soon as the ConnectionInfo constructor is invoked the application exits without any Exception not error message.一旦调用ConnectionInfo构造函数,应用程序就会退出而没有任何异常而不是错误消息。 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?看来我遇到了AccessViolationException ,我知道这是一个严重的问题,不能在托管代码中被捕获,但是......我在使用这个库时做错了什么? I just need to upload my file to a SFTP server.我只需要将我的文件上传到 SFTP 服务器。

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;
    }
}

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 .好吧,我想您在ConnectionInfoPasswordAuthenticationMethod中使用的所有FtpConfiguration conf属性都是string类型,但我认为您缺少必须是intport 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();
}

I think I found a solution.我想我找到了解决办法。 My Upload method was inside a Task async chain.我的Upload方法位于 Task 异步链中。 Getting rid of the async solved the problem.摆脱异步解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM