I'm trying to download a file of ~20 Mb from a SFTP Server and I'm getting this error after ~1 minute from starting.
Exception of type 'System.OutOfMemoryException' was thrown
at Rebex.Net.Sftp.QCW(CRQ U, String W, Stream R, Int64 I, Int64 Q, UWQ C)
at Rebex.Net.Sftp.VQW(String U, Stream W, Int64 R, Int64 I, UWQ Q)
at Rebex.Net.Sftp.GetFile(String remotePath, Stream outputStream)
at ENP.Services.RebexSftpService.GetFile(String filePath) in D:\Projects\ENP\Source\src\Integration\ENP.Services.RebexSftpService.cs:line 53
The code where I have implemented this:
public class RebexSftpService : ISftpService
{
private readonly ILogger _logger;
private bool _disposed;
private Sftp _client;
private string _host;
private string _username;
private string _password;
public RebexSftpService(ILogger logger)
{
_logger = logger;
}
public Sftp SftpClient
{
get
{
if (_client == null)
{
_client = GetSftpClient();
}
if (!_client.IsConnected)
{
_client.Connect(_host);
_client.Login(_username, _password);
}
return _client;
}
}
public SftpResult GetFile(string filePath)
{
var result = new SftpResult { Errors = new List<string>() };
try
{
using (var memStream = new MemoryStream())
{
SftpClient.GetFile(filePath, memStream);
result.File = memStream.ToArray();
}
}
catch (Exception ex)
{
_logger.Log(Level.Critical, ex.Message, ex);
result.Errors.Add(ex.Message);
}
return result;
}
private Sftp GetSftpClient()
{
_host = ConfigurationManager.AppSettings["SftpHost"];
_username = ConfigurationManager.AppSettings["SftpUserName"];
_password = ConfigurationManager.AppSettings["SftpPassword"];
var client = new Sftp
{
Settings = new SftpSettings
{
DisableProgressPercentage = true,
DisablePathNormalization = true,
DisablePutFileZeroOffsetTruncate = true,
DisableRealPathWorkaround = true,
DownloadBufferSize = 48,
RaiseEventsFromCurrentThread = false,
WaitForServerWelcomeMessage = false,
UseLargeBuffers = true,
TryPasswordFirst = true,
DisableFxpStatWorkaround = true,
EnableSignaturePadding = false
}
};
client.Connect(_host);
client.Login(_username, _password);
return client;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (_client != null)
{
if (_client.IsConnected)
{
_client.Disconnect();
}
_client.Dispose();
}
}
// Indicate that the instance has been disposed
_disposed = true;
}
}
I'm disabling all those SftpSettings because the download is faster than not setting any setting. (e.g. no settings = 52 seconds download time. These settings, 22 seconds download time over a random file). But it still takes double time compared to WinSCP, which downloads the same file in 12 seconds.
Thanks!