我正在尝试创建一个全局的WinSCP会话。我需要在我的应用程序的两个不同位置使用会话//Detect Mobile Switch Refill List To Grid
if(window.innerWidth <= 800) {
$scope.view = "MobileAccount";
} else {
$scope.view = "DesktopAccount";
}
和GetFiles
。我的问题是创建会话需要很长时间才能真正减慢应用程序的速度。我尝试了下面的内容来创建一个全局会话,但是当我运行它时会得到一个会话处理异常。是否可以在一个位置打开会话并在应用程序的任何位置使用它。
PutFiles
答案 0 :(得分:2)
您的using
语句会在会话超出范围时立即处理:
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
return session;
} //<-------------------- SESSION DISPOSED HERE!!
移除using
并在所有对象完成会话后手动调用Dispose()
答案 1 :(得分:2)
It's the using
statement that disposes the session, by implicitly calling the Session.Dispose
method. That's its purpose. But you do not want it in your case. Remove it.
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;
}
And make sure you dispose the Session
instance, by explicitly calling the Session.Dispose
method, once it is not needed.
You can, for example, override the OnClosed
method (or handle the Closed
event):
protected override void OnClosed(EventArgs e)
{
GlobalSession.Dispose();
base.OnClosed(e);
}
Though you should really do any session opening/file transferring on a background thread. Not on the GUI thread. But that's a different topic.