SSH.NET异步上传多个文件会引发异常

Aam*_*mir 5 c# sftp task-parallel-library ssh.net

我正在创建一个应用程序

  1. 处理CSV文件,
  2. JObjectCSV文件中的每条记录创建并将JSON保存为txt文件,最后
  3. 将所有这些JSON文件上传到SFTP服务器

在寻找第3点的免费图书馆后,我决定使用SSH.NET.

我创建了以下类来异步执行上载操作.

public class JsonFtpClient : IJsonFtpClient
{
    private string _sfptServerIp, _sfptUsername, _sfptPassword;

    public JsonFtpClient(string sfptServerIp, string sfptUsername, string sfptPassword)
    {
        _sfptServerIp = sfptServerIp;
        _sfptUsername = sfptUsername;
        _sfptPassword = sfptPassword;
    }

    public Task<string> UploadDocumentAsync(string sourceFilePath, string destinationFilePath)
    {
        return Task.Run(() =>
        {
            using (var client = new SftpClient(_sfptServerIp, _sfptUsername, _sfptPassword))
            {
                client.Connect();

                using (Stream stream = File.OpenRead(sourceFilePath))
                {
                    client.UploadFile(stream, destinationFilePath);
                }

                client.Disconnect();
            }
            return (destinationFilePath);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

UploadDocumentAsync方法返回一个,TPL Task以便我可以调用它来异步上传多个文件.

UploadDocumentAsync从以下方法中调用此方法,该方法位于不同的类中:

private async Task<int> ProcessJsonObjects(List<JObject> jsons)
{
    var uploadTasks = new List<Task>();
    foreach (JObject jsonObj in jsons)
    {
        var fileName = string.Format("{0}{1}", Guid.NewGuid(), ".txt");

        //save the file to a temp location
        FileHelper.SaveTextIntoFile(AppSettings.ProcessedJsonMainFolder, fileName, jsonObj.ToString());

        //call the FTP client class and store the Task in a collection
        var uploadTask = _ftpClient.UploadDocumentAsync(
                Path.Combine(AppSettings.ProcessedJsonMainFolder, fileName),
                string.Format("/Files/{0}", fileName));

        uploadTasks.Add(uploadTask);
    }

    //wait for all files to be uploaded
    await Task.WhenAll(uploadTasks);

    return jsons.Count();
}
Run Code Online (Sandbox Code Playgroud)

虽然CSV文件会产生数千条JSON记录,但我想分批上传至少50条记录.这ProcessJsonObjects总是会收到一个50 JObject秒的列表,我想要异步上传到SFTP服务器.但是我client.Connect();UploadDocumentAsync方法的行上收到以下错误:

Session operation has timed out
Run Code Online (Sandbox Code Playgroud)

将批处理大小减小到2可以正常工作,但有时会导致以下错误:

Client not connected. 
Run Code Online (Sandbox Code Playgroud)

我需要能够同时上传许多文件.或者告诉我IIS或SFTP服务器是否需要配置此类操作以及它是什么.

我究竟做错了什么?非常感谢您的帮助.