Monday, November 9, 2009

Error - Multiple calls of CodeAccessPermission.Assert

When dealing with asserting access permissions, a simple line of code may be sufficient. For example like this:

new FileIOPermission(myfilename, 'r').assert();

Above line may be used for assigning rights for reading a CSV file with Ax, with the ASCIIIO class.
But sometimes, this ain't enough and you need to assign more permissions. If you just combine statements like the one above, you may run into following error:

Multiple calls of CodeAccessPermission.Assert

Ax does not support multiple, successive calls to assert in the same calling code. You either have to use the revertAssert method (in between calls), or use assertMultiple.
An example of this last method:

Set permissionSet;
permissionSet = new Set(Types::Class);
permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
permissionSet.add(new FileIOPermission(myfilename1, 'r'));
permissionSet.add(new FileIOPermission(myfilename2, 'rw'));

// do your thing