Aam*_*mir 5 c# sftp task-parallel-library ssh.net
我正在创建一个应用程序
CSV文件,JObject为CSV文件中的每条记录创建并将JSON保存为txt文件,最后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服务器是否需要配置此类操作以及它是什么.
我究竟做错了什么?非常感谢您的帮助.
| 归档时间: |
|
| 查看次数: |
2657 次 |
| 最近记录: |