WPF创建全局WinSCP会话



我正在尝试创建一个全局WinSCP会话。我需要在GetFilesPutFiles的应用程序中的两个不同位置使用会话。我的问题是创建会话需要很长时间,这确实会减慢应用程序的速度。我尝试了下面的方法来创建一个全局会话,但当我运行它时,我得到了一个会话被处理的异常。是否可以在一个位置打开一个会话,并在应用程序中的任何位置使用它。

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    Session GlobalSession;
    private static Session OpenSession()
    {
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Sftp,
        
            HostName = @"server",
            UserName = "name",
            PortNumber = 22,
            SshHostKeyFingerprint = "ssh-rsa 2048 RSAKEY",
        };
        using (Session session = new Session())
        {
            // Connect
            session.Open(sessionOptions);
            return session;
        }
    }
    public MainWindow()
    {
        Session LocalSession = OpenSession();
        GlobalSession = LocalSession;
        InitializeComponent();
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        string path = "";
        try
        {
            // Upload files
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;
            TransferOperationResult transferResult;
            RemoteDirectoryInfo subDirs = GlobalSession.ListDirectory(path);
            foreach(var sub in subDirs.Files)
            {
                MessageBox.Show(sub.ToString());
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}", ex);
        }
    }
    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        try
        {
            // Upload files
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Binary;
            TransferOperationResult transferResult =
                GlobalSession.GetFiles(
                    @"d:toupload*", "/home/user/File", false, transferOptions);
            // Throw on any error
            transferResult.Check();
            // Print results
            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}", ex);
        }
    }
}

它是通过隐式调用Session.Dispose方法来处理会话的using语句。这就是它的目的。但你不希望它出现在你的情况下。移除它。

private static Session OpenSession()
{
    SessionOptions sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Sftp,
        HostName = @"server",
        UserName = "name",
        PortNumber = 22,
        SshHostKeyFingerprint = "ssh-rsa 2048 RSAKEY",
    };
    // Connect
    session.Open(sessionOptions);
    return session;
}

并确保在不需要Session实例时,通过显式调用Session.Dispose方法来处理该实例。

例如,您可以覆盖OnClosed方法(或处理Closed事件):

protected override void OnClosed(EventArgs e)
{
    GlobalSession.Dispose();
    base.OnClosed(e);
}

尽管您确实应该在后台线程上进行任何会话打开/文件传输。不在GUI线程上。但这是另一个话题

一旦会话超出范围,using语句就会处理会话:

 using (Session session = new Session())
 {
    // Connect
    session.Open(sessionOptions);
    return session;
 } //<-------------------- SESSION DISPOSED HERE!!

当所有对象都完成了会话时,删除using并在必要时手动调用Dispose()

相关内容

  • 没有找到相关文章

最新更新