title: SSH.NET Upload Multiple files asynchronously throws an exception
body: <p>I am creating an application that will </p>
  
  <ol>
  <li>process a <code>CSV</code> file,</li>
  <li>create <code>JObject</code> for each record in <code>CSV</code> file and save the JSON as txt file, and finally</li>
  <li>upload all these JSON files to <code>SFTP</code> server</li>
  </ol>
  
  <p>After looking around for a free library for the 3rd point, I decided to use <code>SSH.NET</code>.</p>
  
  <p>I have created the following class to perform the upload operation asynchronously.</p>
  
  <pre><code>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&lt;string&gt; UploadDocumentAsync(string sourceFilePath, string destinationFilePath)
      {
          return Task.Run(() =&gt;
          {
              using (var client = new SftpClient(_sfptServerIp, _sfptUsername, _sfptPassword))
              {
                  client.Connect();
  
                  using (Stream stream = File.OpenRead(sourceFilePath))
                  {
                      client.UploadFile(stream, destinationFilePath);
                  }
  
                  client.Disconnect();
              }
              return (destinationFilePath);
          });
      }
  }
  </code></pre>
  
  <p>The <code>UploadDocumentAsync</code> method returns a <code>TPL Task</code> so that I can call it to upload multiple files asynchronously.</p>
  
  <p>I call this <code>UploadDocumentAsync</code> method from the following method which is in a different class:</p>
  
  <pre><code>private async Task&lt;int&gt; ProcessJsonObjects(List&lt;JObject&gt; jsons)
  {
      var uploadTasks = new List&lt;Task&gt;();
      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();
  }
  </code></pre>
  
  <p>Although the CSV file results in thousands of JSON records, but I want to upload these in batches of at least 50. This <code>ProcessJsonObjects</code> always receives a list of 50 <code>JObject</code>s at a time which I want to upload asynchronously to the SFTP server.  But I receive the following error on <code>client.Connect();</code> line of the <code>UploadDocumentAsync</code> method:</p>
  
  <pre><code>Session operation has timed out
  </code></pre>
  
  <p>Decreasing the batch size to 2 works fine but sometimes results in the following error:</p>
  
  <pre><code>Client not connected. 
  </code></pre>
  
  <p>I need to be able to upload many files at the same time. Or tell me if IIS or SFTP server needs configuration for this type of operation and what is it.</p>
  
  <p>What am I doing wrong? Your help is much appreciated.</p>