Monday, February 8, 2010

Error: Multiple calls to CodeAccessPermission.Assert

Multiple calls to CodeAccessPermission.Assert
(S)\Classes\SkipAOSValidationPermission\assert
(S)\Classes\BatchRun\runJob - line 166
(S)\Classes\BatchRun\do - line 54
(C)\Forms\BatchRun\Methods\doBatch - line 18

Sometimes I got this error while working with Change based alerts batch processing. After that the status of the related job in batch list changed to Executing, and the only way to get rid of it was deleting that job.


This issue arises in [Classes]BatchRun.runJob method because of absence of closing revertAssert() method in case of exceptions after calling runas() method.

So, my solution is to comment the existing call of revertAssert()...

if (batchClass.runsImpersonated())
        {
            // Ok to assert here because the user name comes from
            // the batch table
            runAsPermission = new RunAsPermission(batch.CreatedBy);
            runAsPermission.assert();
            // BP Deviation Documented
            runas(batch.CreatedBy,
                classnum(BatchRun),
                staticmethodstr(BatchRun, runJobStatic),
                [batchId]);

           // Alexey Voytsekhovskiy (SIS) (2010/02/08) (Demande #0074)
           // CodeAccessPermission::revertAssert();
        }
        else
        {
            BatchRun::runJobStatic([batchId]);
        }

and move it after catching all kind of exceptions:


catch (Exception::UpdateConflictNotRecovered)
    {
        isErrorCaught   = true;
        exception       = Exception::UpdateConflictNotRecovered;
    }

    // Alexey Voytsekhovskiy (SIS) (2010/02/08) (Demande #0074)
    // in case of exception Permssion should be revert anyway!!
    CodeAccessPermission::revertAssert();

It allows to avoid multiple calls to CodeAccessPermission.Assert (there is another call far in this method) and see the log of the job ended with an error.


By the way, here is a short and sweet trick with multiple calls if needed.

2 comments:

Sanjivkumar Kori said...

Hi,

Please let me know which Ax version was this where you face this issue.

Sanjivkumar Kori said...
This comment has been removed by the author.