Dynamics AX file handling using batch server

Hi,

I am working on an import functionality that includes browsing a web resource, searching for all files located in the resource, reading them and moving them to another resource - all executed by a batch server. The reading part goes well, but all the other seem to be not working. I started by using WinAPI methods (FindFirstFile and FindNext File), but I’ve read that they won’t work under batch server, so I changed them to something like this:

System.IO.DirectoryInfo di;
System.Type arrayType;
System.Array array;
System.IO.FileInfo fi;
int i;
int l;

di = new System.IO.DirectoryInfo(filePath);
arrayType = System.Type::GetType(“System.IO.FileInfo”);
array = System.Array::CreateInstance(arrayType, 1);
array = di.GetFiles();
l = array.get_Length();
for (i = 0; i < l; i++)
{
fi = array.GetValue(i);
fileNameOpen = fi.get_FullName();
this.readFile();
}

According to what I’ve read on the net, that should work. And it does - it finds all the files in the directory stated in the filePath and then executes the readFile() method that uses the global fileNameOpen variable. The problem is - it only works when I run it manually. Somehow, the batch server doesn’t execute it properly, I know the batch job is executed (it’s on the executed batch job list and it worked properly when I read the files by directly stating the fileNameOpen variable) but the searching itself doesn’t work. File access rights are ok, I checked that. Could it have something to do with the fact that I’m using System classes? Or is there anything else that I could have missed?

Hi,

The issue lies with opening the file. If you want the file to be opened on client, use client key word. See here for more info - http://dynamicsuser.net/forums/p/27424/146753.aspx#146753

Hope this helps,

Also see this post from X++ team regarding debugging batch jobs in Dynamics Ax - http://blogs.msdn.com/x/archive/2009/06/25/step-by-step-checklist-for-debugging-batch-jobs-in-dynamics-ax.aspx

Hmm… I think there is a requirement for FileIOPermission while trying to access directory methods, not only the files themselves. I’ll check it tomorrow when my batch job runs and I’ll post the results.

you have to insert fileiopermission, it need when you access file with IO

Yes, that seems to be the case, furthermore InteropPermission seems to be needed, but since there was too much trouble, I’ve changed the model to something more primitive, but also more safe - I’m done testing in on a production instance. :wink:

If the issue is resolved, it will be interesting to know the solution. Perhaps it might be useful for somebody some day. Therefore if you can, do please let us know how you resolved it.

Thanks,

When I wrote the last statement, I was thinking to skip browsing the folder and moving the files, and instead just read them (since I new what their names would be anyway) and mark them as read in a dedicated table.

Instead, I came up with the real solution. It took me an evening of work, but it works in 100%. Here are the conclusions:

  • The class that is ran as a batch-job, has to be ran on Client.

  • The FileIOPermission and InteropPermission have to be assigned simutanously in order to both read the files and use the .NET classes.

The run() method of a class that I wrote to test this, is attached below. It scans folder A and moves files from folder A to folder B.

public void run()
{
System.IO.DirectoryInfo di;
System.Type arrayType;
System.Array array;
System.IO.FileInfo fi;
FilePath filePath, moveFilePath, shortFile;
FileNameOpen fileNameOpen;
FileIOPermission dirPermission, filePermission;
InteropPermission interopPermission;
Set permissionSet;
int i;
int l;

;

super();

interopPermission = new InteropPermission(InteropKind::ClrInterop);
filePath = ‘\\bdtax40msp\luk\a\’;
dirPermission = new FileIOPermission(filePath, ‘R’);
permissionSet = new Set(Types::Class);
permissionSet.add(interopPermission);
permissionSet.add(dirPermission);
CodeAccessPermission::assertMultiple(permissionSet);

di = new System.IO.DirectoryInfo(filePath);
arrayType = System.Type::GetType(“System.IO.FileInfo”);
array = System.Array::CreateInstance(arrayType, 1);
array = di.GetFiles();
l = array.get_Length();
CodeAccessPermission::revertAssert();
if (l > 0)
{
for (i = 0; i < l; i++)
{
interopPermission = new InteropPermission(InteropKind::ClrInterop);
interopPermission.assert();
fi = array.GetValue(i);
fileNameOpen = fi.get_FullName();
CodeAccessPermission::revertAssert();
filepermission = new FileIOPermission(fileNameOpen, ‘W’);
interopPermission = new InteropPermission(InteropKind::ClrInterop);
permissionSet = new Set(Types::Class);
permissionSet.add(interopPermission);
permissionSet.add(dirPermission);
CodeAccessPermission::assertMultiple(permissionSet);
shortFile = fi.get_Name();
moveFilePath = “\\bdtax40msp\luk\b\” + shortFile;
fi.MoveTo(moveFilePath);
CodeAccessPermission::revertAssert();
}
}
}

Hi, i have added a filePermissiong to the permission set

permissionSet.add(filePermission);

but still i am not able to move files from one folder to another folder. Is there any other way to do this job.

Thanks

Hi,

Is it possible to run Comma IO file on batch server???

Hi Varaprasad,

Kindly assign the full access permission to the “Network Service” user on all of the folders where you want to copy files. It will work.

regards,

Faisal Fareed