none
FTP Upload slow after move from .NET 2.0 to .NET 4.6 RRS feed

  • Question

  • Hi,

    We have an .NET component that uploads file to ftp server. We developed this in .NET 2.0 and deployed in Windows 2003 R2 while back. Component works fine and no issue. We are upgrading our system. So we rebuild this same code/component in .NET 4.6 and deployed it on Windows 2016. We are having  performance issue now. When use this program to move 7500 files to destination (it upload 10 to 20 files at a time based on business case) we are getting 451 error from ftp server. It appears that Upload streaming is very slow and take too long to complete uploading files regardless of size. Exact same code that runs on .NET 2.0, with same ftp server and same number of files, works fine without any issue. Not sure why this issue with upper version. Any thoughts?

    Here is our code.

    private Uri CreateFtpUri(string folderName, string fileName)
            {
                string path = (folderName == string.Empty) ? fileName : Path.Combine(folderName, fileName);
                UriBuilder builder = new UriBuilder("ftp", _hostName, _port, path);
                builder.UserName = _userName;
                builder.Password = _password;

                return builder.Uri;
            }

         
            /// Uploads file to remote server
       
     /// Buffersize 4096
            public long Upload(Stream stream, string remoteFolderName, string remoteFileName, int bufferSize)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("FTPClient.Upload to {0}/{1} START", remoteFolderName,remoteFileName));

                long totalBytes = 0;
                long position = -1;
                if (stream.CanSeek)
                {
                    position = stream.Position;

                }

                Uri uri = CreateFtpUri(remoteFolderName, remoteFileName);
                FtpWebRequest request = CreateRequest(uri);
                request.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

                byte[] buffer = new byte[bufferSize];

                using (Stream requestStream = request.GetRequestStream())
                {
                    int i;
                    do
                    {
                        i = stream.Read(buffer, 0, bufferSize);
                        if (i > 0)
                        {
                            requestStream.Write(buffer, 0, i);
                            totalBytes += i;
                        }
                    }
                    while (i > 0);

                    requestStream.Close();

     
                    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                    {
                        response.Close();
                    }
                }

                if (position >= 0)
                {
                    stream.Position = position;
                }

                System.Diagnostics.Debug.WriteLine(string.Format("FTPClient.Upload to {0}/{1} COMPLETE", remoteFolderName, remoteFileName));

                return totalBytes;
            }

    Sunday, December 17, 2017 10:28 PM

All replies

  • Hi WatNxt,

    Thank you for posting here.

    For your question, I could test your code. The CreateRequest() does not exist.

    Please provide the sample code which could reproduce the error for us to test.

    As I know, the exception may caused by the difference between .net framework 2.4 and .net framework 4.0. You would like to check the source code.

    https://referencesource.microsoft.com/

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 18, 2017 8:14 AM
    Moderator
  • Hi Wendy,

    Thank you and here are other part of codes.       

    private string _hostName;
            private string _userName;
            private string _password;
            private int _port;
            private bool _keepAlive;
            private bool _usePassive;
            private int _connectionLimit;
            private int _timeoutMs;
            private bool _useBinary;
      
      
      public FTPClient(string hostName, string userName, string password, int port, bool keepAlive, bool usePassive, int connectionLimit, int timeoutMS, bool useBinary)
            {
                _hostName = hostName;
                if (_hostName.IndexOfAny(new char[] { ':', '\\', '/' }) >= 0)
                {
                    throw new ApplicationException("Host name contains illegal chars.");
                }

                _userName = userName;
                if (_userName == null || _userName == string.Empty)
                {
                    throw new ApplicationException("User name is null or empty");
                }

                _password = password;
                _port = port;
                _keepAlive = keepAlive;
                _usePassive = usePassive;
                _connectionLimit = connectionLimit;
                _timeoutMs = timeoutMS;
                _useBinary = useBinary;
            }
            private FtpWebRequest CreateRequest(Uri uri)
            {
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(uri);
                request.Credentials = new NetworkCredential(_userName, _password);
                request.KeepAlive = _keepAlive;
                request.UsePassive = _usePassive;
                request.ServicePoint.ConnectionLimit = _connectionLimit;
                request.Timeout = _timeoutMs;
                request.UseBinary = _useBinary;
                return request;
            }

    Monday, December 18, 2017 6:50 PM
  • Hi WatNxt,

    Thank you for the part of the code.

    But it still has compiler error.

    There is no use of FTPClient method. 

    If you could not provide the project for security, could you provide the line of the code which you get the exception? I will compare the source code of the method which you get the exception from .net framework 2.0 to .net framework 4.6.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, December 20, 2017 8:39 AM
    Moderator
  • Hi Wendy,

    Do you work for Microsoft (Reason I am asking, we talked with a MS person name Wendy during this project and I am wondering if that is you)? If it so, would it be possible to provide me your contact info so I can send you the code?

    Thursday, December 21, 2017 6:54 PM
  • Hi WatNxt,

    According to our policy, we could not provide our personal contact information. You could upload on OneDrive. I will download to test.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, December 22, 2017 2:48 AM
    Moderator
  • Hi Wendy,

    Todd has full access to our code and he can give you access to our source code in VS as well. Could you please reach him to get the access?

    Friday, December 22, 2017 1:46 PM
  • Hi WatNxt,

    >>Todd has full access to our code and he can give you access to our source code in VS as well. Could you please reach him to get the access?

    What does this mean?

    According to our policy, please upload the project which could reproduce the error to OneDrive. Or you could provide the line of the code which caused the error, I will check the .net framework source code of different version.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 26, 2017 1:29 AM
    Moderator