【问题标题】:FTPWebRequest in Batch批量 FTPWebRequest
【发布时间】:2023-07-13 14:06:02
【问题描述】:

我创建了一个批处理类来检查 FTP 文件,下载它们并在 FTP 上删除它们。

当我手动运行它(不是批量运行)时,它可以完美运行并下载 FTP 中的所有文件,并在下载完成后将其删除。

当我尝试批量运行时,问题就开始了,我尝试了服务器端和客户端批处理。

他们都给出了一个超时错误:

System.Reflection.TargetInvocationException:调用的目标已抛出异常。 ---> System.Net.WebException: 操作已超时。

在 System.Net.FtpWebRequest.CheckError()

在 System.Net.FtpWebRequest.GetResponse()

---内部异常堆栈跟踪结束---

有人有从 FTP 批量下载文件的经验吗?

我已尝试将超时时间设置得更高。我还测试了执行批处理作业的服务器上的连接,我可以访问 FTP。所以这不是防火墙问题。我认为它一定是 AX 内部的东西,但我真的什么都想不出来。

这是代码(注意:downloadfile 和 deletefile 是用不同的 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();
        }
    }
}

这是完全的例外:

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)    

【问题讨论】:

  • 能否将生成的错误保存到文件中,例如? dynamicsaxgyan.wordpress.com/2011/04/07/…
  • 帖子中添加了完整的异常。这就是我能从中得到的所有信息。
  • 检查 从执行 Ax 服务的服务器是否可见。也许它不仅仅是服务器名称,而是传输方法(不能从服务器上的端口 20/21 进行 ftp)。检查以下问题是否得到解决*.com/questions/16196195/dynamics-ax-ftp-x

标签: axapta dynamics-ax-2009 ftpwebrequest


【解决方案1】:

根据我的经验,批量运行时文件操作不可靠。如果您尝试在客户端上运行它并且仍然超时,那么您可能会遇到另一个问题。根据我的经验,您可能需要打开 FTP 服务器的属性(很可能在 IIS 中)并增加超时限制。我不得不对我们用来产生销售税的网络服务做类似的事情。如果我们有一个超过 100 行的销售订单,它会产生同样的错误。当我们增加超时时,错误就消失了。

【讨论】: