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.

Options
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)

Options

Topic review

DaveT

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

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).

See https://winscp.net/eng/docs/library_session_getfile#remarks
DaveT

Session.GetFile throws "Recursive calls not allowed"

Hi,
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:

Exception:
System.InvalidOperationException: 'Recursive calls not allowed'

Stack:
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.Open(sessionOptions);
 
    session.DebugLogPath = @"c:\test\log.txt";
 
    var remoteFile = session
        .ListDirectory(remotePath)
        .Files
        .Where(x => x.IsDirectory == false)
        .OrderByDescending(x => x.LastWriteTime)
        .FirstOrDefault();
 
    if (remoteFile != null)
    {
        sftpFile.Name = remoteFile.Name;
 
        sftpFile.Content = session.GetFile(remoteFile.FullName);
    }
 
    session.Close();
}

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