1

Closed

Disconnect() hangs in ASP.Net context

description

Try this from the code behind of a regular aspx file. The file uploads fine, but the code just stops at the sftp.Disconnect() call.

string fileContents = "sftp this text!";
    Stream contentStream = new MemoryStream(ASCIIEncoding.Default.GetBytes(fileContents));
 
    string filenameFormat = "filename_{0:yyyyMMdd_HHmm}.txt";
    string filename = string.Format(filenameFormat, DateTime.Now);
 
 
    bool fileUploaded = false;
    using (SftpClient sftp = new SftpClient("sftp.MYDOMAIN.com", 5970, "MYUSERNAME", "MYPASSWORD"))
    {
        sftp.Connect();
 
        sftp.UploadFile(contentStream, filename);
 
        // calling disconnect kills everything - removing it just so the code will continue
 
        try
        {
            sftp.Disconnect();
        }
        catch
        {
 
        }
        // CODE NEVER GETS HERE
        Response.Write("IT WORKED!");
    }
Closed Sep 12, 2011 at 8:27 PM by olegkap
No response for long time

comments

rafekemmis wrote Aug 1, 2011 at 4:48 PM

This issue is for the 7/29 build.

olegkap wrote Aug 2, 2011 at 7:30 PM

Hi,
I just did a test project and it works fine for me and does not hung.
I used latest source code(current release).

Here is a code I used:
namespace Test.Web
{
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var connectionInfo = new PasswordConnectionInfo("152.54.xx.xxx", 22, "username", ".....");
        using (var sftp = new SftpClient(connectionInfo))
        {
            sftp.Connect();
            sftp.UploadFile(File.OpenRead(@"C:\Test\10MB.bin"),"test.bin");
            sftp.Disconnect();
        }
    }
}
}


Can you try it again or may be send me you project example ( I can use my SSH test server)
Thanks,
Oleg

rafekemmis wrote Aug 4, 2011 at 10:58 PM

This function lives inside my business logic class, which gets called from within a simple generic http handlers (.ashx file):
public void ftpContacts()
{       
    StringBuilder s = new StringBuilder();

    // do some stuff here to build up a sring using s

    string fileContents = s.ToString();
    Stream contentStream = new MemoryStream(ASCIIEncoding.Default.GetBytes(fileContents));

    string filenameFormat = "my_file_prefix_{0:yyyyMMdd_HHmm}.txt";
    string filename = string.Format(filenameFormat, DateTime.Now);


    bool fileUploaded = false;
    // note, i am connecting to port 5970, not sure that matters
    using (SftpClient sftp = new SftpClient("sftp.xxxx.com", 5970, "username", "pass"))
    {
        sftp.Connect();
        sftp.UploadFile(contentStream, filename);
// at this point my file is uploaded just fine
        sftp.Disconnect();
// code execution hangs on Disconnect() call - eventually the call to my ashx file times out.
        fileUploaded = true;
    }



    if (fileUploaded)
    {
       // remove the data from the db that was just transfered
    }

}

rafekemmis wrote Aug 4, 2011 at 11:27 PM

I just tried my code against another sftp server and it seemed to work okay. Any ideas what might be the problem when going against my other server? Maybe I need to change some settings on the sftpclient before I connect?

olegkap wrote Aug 24, 2011 at 3:37 PM

Sorry for late response,
I dont know what might be a problem.
If you still experience the problem, can you try to Debug and see what line it hangs so may be I could get an idea about what happening.

Thanks,
Oleg

jkhouw wrote Feb 10, 2012 at 10:35 PM

Hmm - hangs for me using c# console app at disconnect and using latest (dec 2011) build.