3

I made a batch class to check an FTP for files, download them and delete them on the FTP.

When I run it manually (not in batch) it works perfectly and downloads all files in the FTP and deletes them when the downloading is done.

The problem starts when i try to run this in batch, I tried both serverside and client batches.

Both of them give a timout error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.WebException: The operation has timed out.

at System.Net.FtpWebRequest.CheckError()

at System.Net.FtpWebRequest.GetResponse()

--- End of inner exception stack trace ---

Does anyone have any experience downloading files from an FTP in batch?

I have tried setting the timeout time higher. I also tested the connection on the servers executing the batch-job and i can access the FTP. So it's not a firewall issue. I think it must be something within AX but I can't really think of anything.

this is the code (note: downloadfile and deletefile is the same code to make the connection with a differenct set_method():

permissionSet =  new Set(Types::Class);
files = new List(types::String);
permissionset.add(new InteropPermission(InteropKind::DllInterop));
permissionset.add(new InteropPermission(InteropKind::ClrInterop));
CodeAccessPermission::assertMultiple(permissionset);
ftpo = System.Net.WebRequest::Create(<ftp link>);
request = ftpo;
request.set_KeepAlive(false);
request.set_UsePassive(false);
request.set_UseBinary(true);
request.set_Method("NLST");
credential = new System.Net.NetworkCredential(<user>,<pw>);
request.set_Credentials(credential);
try
{
    //first get the filelist from FTP
    response = request.GetResponse();
    reader = new System.IO.StreamReader(response.GetResponseStream());
    while(!reader.get_EndOfStream())
    {
        text = reader.ReadLine();
        files.addStart(text);
    }
    reader.Close();
    response.Close();
    CodeAccessPermission::revertAssert();
    if(files.elements() >0)
    {
        it = New ListIterator(files);
        while(it.more())
        {
            filename = it.value();
            downloadfile(filename);
            deleteFile(filename);
            it.next();
        }
    }
}

This is the full exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.WebException: The operation has timed out.
   at System.Net.FtpWebRequest.CheckError()
   at System.Net.FtpWebRequest.GetResponse()
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at ClrBridgeImpl.InvokeClrInstanceMethod(ClrBridgeImpl* , ObjectWrapper* objectWrapper, Char* pszMethodName, Int32 argsLength, ObjectWrapper** arguments, Boolean* argsAreByRef, Boolean* isException)    
AnthonyBlake
  • 2,334
  • 1
  • 25
  • 39
  • Can you save the error generated to file, for example? http://dynamicsaxgyan.wordpress.com/2011/04/07/clrinterop-in-x-and-managing-exceptions-dynamics-ax/ – ian_scho Apr 24 '13 at 06:42
  • Full exception is added to the post. That's about all information i can get out of it. – Roderik Latruwe Apr 25 '13 at 09:14
  • Check if the is visible from the server which the Ax service is executing from. Maybe it's not just server name but the transport method (cannot ftp from port 20/21 on your server). Check out if the following gets resolved http://stackoverflow.com/questions/16196195/dynamics-ax-ftp-x – ian_scho Apr 27 '13 at 06:40

1 Answers1

1

In my experience file operations are not reliable when run in batch. If you try to run it on the client and it still times out then you may have another issue. In my experience, you may need to open up the properties for your FTP server (most likely it will be in IIS) and increase the time out limit. I had to do something similar with a web service that we use to generate sales tax. If we had a sales order with more than 100 lines, it would generate the same error. When we increased the timeout, the error went away.

Michael Brown
  • 2,221
  • 2
  • 17
  • 34