WinSCP Automation - Connection has been unexpectedly closed. Server sent command exit status
Hello,
This is my very first post here and I am very much impressed by the simplicity of the ways to work with the WinSCP client. However, as always there are challenges, that need to be overcome and sometimes get better of you. So here it goes...
We are currently working on replacing a certain file transfer across the network using Windows shared paths by invoking WinSCP and thus automating the existing manual process with SFTP. So in the existing application, I have created a class to expose the relevant APIs and it works fine the first time. However, the problem starts when I try to initiate another set of file transfers. I am presented with the exception:
Connection has been unexpectedly closed. Server sent command exit status 0.
So in my application, I am not instantiating the session and its related members using the sample code. Instead, we are loading WinSCPNet.dll dynamically at runtime. 2 reasons for doing this:
[3] How do I set the transfers to happen in back ground via C#? And how do I handle concurrency? It may so happen that the user may launch multiple instances of the .NET application and try to do multiple transfers simultaneously. What should happen in that scenario as one session running under existing instance would have transfers scheduled and user would try to pass in same credentials from another application instance to create another session.
I have tried to put the details in a best possible manner so that it is easy to understand the scenario. Kindly assist me further to understand the pitfalls at the earliest.
Regards,
Bhushan
This is my very first post here and I am very much impressed by the simplicity of the ways to work with the WinSCP client. However, as always there are challenges, that need to be overcome and sometimes get better of you. So here it goes...
We are currently working on replacing a certain file transfer across the network using Windows shared paths by invoking WinSCP and thus automating the existing manual process with SFTP. So in the existing application, I have created a class to expose the relevant APIs and it works fine the first time. However, the problem starts when I try to initiate another set of file transfers. I am presented with the exception:
Connection has been unexpectedly closed. Server sent command exit status 0.
So in my application, I am not instantiating the session and its related members using the sample code. Instead, we are loading WinSCPNet.dll dynamically at runtime. 2 reasons for doing this:
- The usual add reference mechanism always kept giving me exceptions as I tried adding reference and specifying the executable path as well as I tried adding the assembly directly from the WinSCP install location.
- Another reason to go for the overhead of not adding a reference and writing the extra code to load the assembly at runtime was to put the onus of installation of WinSCP on the end user and assuming this makes clear distinction of the responsibilities to be handled.
Approach 2 is working for me in a simple scenario.
- As I do one complete cycle of uploading files to the predefined SFTP location, I close the connection (and never dispose it). I have put
session.Opened
checks before starting the new cycle to check and open a new session to start a new cycle which is failing with the error:
Connection has been unexpectedly closed. Server sent command exit status 0.
So what has to be done to get past it? Just to add, I cannot use the braces to dispose the session object as I have declared it in a wrapper class and this class has methods exposed to the outside world to do stuff. I have not even implemented Dispose yet and with the application still running, we were not expecting this error. If it is about Session.Timeout / ReconnectionTime or the relevant options in SessionOptions, kindly assist me as to how do I set these properly, so that the session never times out or atleast stays alive till the user COB.
- How should I properly call
Dispose
on all the relevant WinSCP objects, that I am creating using "dynamic" type in C#? I intend to do it when the user exits the application. For example,
public class WinSCPExtension { private dynamic sessionOptions public dynamic session private dynamic AnyOtherWinSCPSpecificObject public void InitializeSession() { // called from outside the class... myWinSCPNetAssembly = Assembly.LoadFrom(PathToWinSCPNetdll)); var sessionOptionsType = myWinSCPNetAssembly.GetType("WinSCP.SessionOptions"); sessionOptions = Activator.CreateInstance(sessionOptionsType); ... ... sessionOptions.HostName = hostName; sessionOptions.UserName = userName; sessionOptions.Password = pwd; sessionOptions.SshHostKeyFingerprint = SftpSshHostKeyFingerprint; var sessionType = myWinSCPNetAssembly.GetType("WinSCP.Session"); session = Activator.CreateInstance(sessionType); session.ExecutablePath = PathToWinSCPEXE; .... } public void OpenSession() { .... if (!session.Opened) session.Open(sessionOptions); .... .... // Do more stuff... } public void CloseSession() { if(session.Opened) session.Close(); } ... // Presently there is no dispose implemented. }
I have tried to put the details in a best possible manner so that it is easy to understand the scenario. Kindly assist me further to understand the pitfalls at the earliest.
Regards,
Bhushan