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'));
CodeAccessPermission::assertMultiple(permissionSet);

// do your thing

CodeAccessPermission::revertAssert();

6 comments:

  1. Solved my problem! Thankyou!

    ReplyDelete
  2. exactly what i was looking for. thank you

    ReplyDelete
  3. Excellent! Thank you.

    ReplyDelete
  4. Still usefull in 2017 :-)

    ReplyDelete
  5. Hi sir ,
    can you give some idea to select and update the records in the external database through the X++

    ReplyDelete