FtpWebRequest locks up web applications on server

I think my code is locking up the server when using FtpWebRequest to upload a large file to a remote server. The attached code is run inside an ASP.NET application.  Could someone please review the code and let me know if I am making any obvious mistakes?

What is happening is that the ASP.NET application that this code is part of ends up locking up another, non-ASP.NET application that runs in a separate thread.  By locking up, I mean that if I open:

http://www.server.com/application1.aspx 

which runs the code below and, at the same time I run:

http://www.server.com/application2/open_page.stt (runs via a separate ISAPI extension)

then the second application does not respond until the file in the first application has been uploaded.

Please let me know if I am doing something wrong in how I use FtpWebRequest or how I could troubleshoot this...  :\

Thanks,
Nikita
private void Upload(string filename, string ftpServer, string ftpUserID, string ftpPassword)
        {
            FileInfo fileInf = new FileInfo(filename);
            string uri = "ftp://" + ftpServer + "/" + fileInf.Name;
            FtpWebRequest reqFTP;
            // Create FtpWebRequest object from the Uri provided
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServer + "/" + fileInf.Name));
            // Provide the WebPermission Credintials
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            // By default KeepAlive is true, where the control connection is not closed
            // after a command is executed.
            reqFTP.KeepAlive = false;
            // Specify the command to be executed.
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            // Specify the data transfer type.
            reqFTP.UseBinary = true;
            // Notify the server about the size of the uploaded file
            reqFTP.ContentLength = fileInf.Length;
            // The buffer size is set to 2kb
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
            FileStream fs = fileInf.OpenRead();
            try
            {
                // Stream to which the file to be upload is written
                Stream strm = reqFTP.GetRequestStream();
                // Read from the file stream 2kb at a time
                contentLen = fs.Read(buff, 0, buffLength);
                // Till Stream content ends
                Response.Write("<BR/>Uploading");
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload Stream
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
 
                }
                // Close the file stream and the Request Stream
                strm.Close();
                fs.Close();
            }
            catch (Exception ex)
            {
                Response.Write("<p><font color='red'>Error Uploading File</font></p>");
            }
        }

Open in new window

AkitinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rahul Goel ITILSenior Consultant - DeloitteCommented:
I thnk you must use the windows service or DTS/SSIS to do that. Using web application you can put that file in the specific folder and intimate the user that file has been marked to upload and it will upload in next 30 mins.  and windows service or dts could be fired to do operation.
That's the best approach I must suggest you
0
AkitinAuthor Commented:
Thanks for that Rahu, but I would like to try to figure out why this is not working as I would like to display confirmation of successful upload.  Also, I'd like to understand why this is happening so that I can make sure that I take all precautions necessary in my applications to prevent lockups.
0
AkitinAuthor Commented:
WhackAMod,

After some more research, I found that it might not be the FTP code locking up the server and perhaps that code was written correctly.

Nikita
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Networking Protocols

From novice to tech pro — start learning today.