Multiple calls to CodeAccessPermission.Assert error in ax 2012

Hello guys,maximum people will write their code like below statements to create 2 text files at a time.

/*
fileIOPermission = new FileIOPermission(filename,'w');
fileIOPermission.assert();
textIo = new TextIo(filename, 'w');

fileIOPermission1 = new FileIOPermission(filename1,'W');
fileIOPermission1.assert();
textIo1 = new TextIo(filename1, 'w');
*/

The above statements will create a problem like"Multiple calls to CodeAccessPermission.Assert ".

To solve,Please change your code as below.

static void Job21(Args _args)
{  
        Set   permissionSet;
        TextIo    textIo,textIo1;
        str filename,filename1;

        filename = @'D;\testfile1.txt';
        filename1=@'D:\testfile2.txt';

        //creating Set Class Object of type class.
        permissionSet = new Set(Types::Class);

       //adding FileIOPermission objects to Set.
        permissionSet.add(new FileIOPermission(filename,'w'));
        permissionSet.add(new FileIOPermission(filename1,'w'));

         //Permissions are set -- multiple              
        CodeAccessPermission::assertMultiple(permissionSet);

        //creating textIO class objects.
        textIo              = new TextIo(filename, 'w');
        textIo1             = new TextIo(filename1,'w');

        //writing data to the file.
        textIo.write('test');
        textIo1.write(testing');

        //making null is nothing but removing object from memory.
        textIo  = null;
        textIo1 = null;
        CodeAccessPermission::revertAssert();
}

Hope this helps you guys.
Thanks.



Comments

Popular posts from this blog

x++ code to add hyperlink to a field in D365FO

x++ code to get Vendor Email Address based on Purpose in Dynamics 365 for Operations

x++ code to get Customer Address based on purpose in ax 2012