Ashish Kumbhar
Ashish Kumbhar

Reputation: 21

Read FTP file from Azure VM or Azure WebJob

I have created a console application which reads a CSV file from FTP server and dump the records after validation to database.

When I tried the code from my local machine, its working perfectly. No errors, no bugs. But when I put it on a Azure Virtual Machine to schedule it. OR When I create a Azure WebJob to create a schedule Job, I am getting following error while accessing the file.

The remote server returned an error: (500) Syntax error, command unrecognized. at System.Net.FtpWebRequest.SyncRequestCallback(Object obj) at System.Net.FtpWebRequest.RequestCallback(Object obj) at System.Net.CommandStream.Dispose(Boolean disposing) at System.IO.Stream.Close() at System.IO.Stream.Dispose() at System.Net.ConnectionPool.Destroy(PooledStream pooledStream) at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse) at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) at System.Net.FtpWebRequest.GetResponse() at Bethesda_DataUpload.Program.Main(String[] args) in D:\Projects\Program.cs:line 35

I am using following code to connect to FTP server

    string dataFile = "ftp://testftp-L.cloudapp.net/Inbound/sampleFile.csv";
    FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(dataFile));
    request.UsePassive = false;
    request.UseBinary = true;
    request.KeepAlive = true;
    request.Credentials = new NetworkCredential(ftpUsrName, ftpPassword);
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.Proxy = WebRequest.DefaultWebProxy;
    request.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

    List<string> entries = new List<string>();


    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    {
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            entries = reader.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
        }
    }

I have tried all the combinations of KeepAlive and UsePassive like

Combination 1

request.UsePassive = false;
request.KeepAlive = false;

Combination 2

request.UsePassive = false;
request.KeepAlive = true;

Combination 3

request.UsePassive = true;
request.KeepAlive = false;

Combination 4

request.UsePassive = true;
request.KeepAlive = true;

What code I should use?

Upvotes: 2

Views: 372

Answers (0)

Related Questions