Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)


Topic review


Thanks Martin! Indeed, I forgot to dispose the stream...

Re: Session.GetFile throws "Recursive calls not allowed"

I do not know what your sftpFile.Content setter does. But you need to dispose the stream returned by Session.GetFile, before you can use the Session instance again. That's true even for calls to Close or Dispose (implied by the using).


Session.GetFile throws "Recursive calls not allowed"

I'm using the WinSCP 5.18.2-beta Version and I want to download a file via SFTP protocol to stream.

For .NET Standard 2.0 as well as for .NET Framework I´m getting the same errors.

After calling session.GetFile the stream has errors you can see in the attached file StreamErrors.PNG.

Than after calling session.Close the following exception is thrown:

System.InvalidOperationException: 'Recursive calls not allowed'

at WinSCP.Lock.Enter()
at WinSCP.Session.Dispose()
at ..DownloadFile(String remotePath) in ..\Sftp.cs:line 97

The code I´m using is the following:
using (var session = new Session())
    var sessionOptions = this.InitializeSessionOptions();
    session.DebugLogPath = @"c:\test\log.txt";
    var remoteFile = session
        .Where(x => x.IsDirectory == false)
        .OrderByDescending(x => x.LastWriteTime)
    if (remoteFile != null)
        sftpFile.Name = remoteFile.Name;
        sftpFile.Content = session.GetFile(remoteFile.FullName);

I have also tried to use session.GetFileToDirectory and it works perfectly fine.

So I don´t know if there´s something wrong with my code or could it be something else?

Best, David