[ Microsoft Dynamics AX 2012 ] CIL generation: Source array was not long enough. Check srcIndex and length, and the array’s lower bounds

Error

CIL generation: Line Number nn - Line Number nnn - Source array was not long enough. Check srcIndex and length, and the array's lower bounds.    \Classes\WhateverClass\WhateverMethod    nn    whatever    Err:nnn

Solution

On the Internet, there’s always a suggest to do a magic trick ( `Generate Full CIL` ) that solved all the symptom of Dynamics AX.

However, In my case, the `Generate Full CIL` does not help.

Scenario 1

In my case, The AX throws error `Source array was not long enough. Check srcIndex and length` and AX point the red underline error to class A,

A — use —> B

AX

indicate error

However, the root cause of the error is in class B that is used by class A. The class B has compile error. ( I knew this by compile class B again, and found that some fields that is used in class B are missing from the table ).

After comment it out, recompile class B, and `generate increment CIL`. The error `Source array was not long enough. Check srcIndex and length, and the array’s lower bounds` is gone.

NOTE here that I don’t need to `generate full CIL` at all.

Scenario 2

I also found that, in some case AX indicates error in class A with `Source array was not long enough. Check srcIndex and length`, which class A use class B that extend from class C.

A —- use —-> B ( extends C )

Ax

indicates error

The root cause of the error is in class C. The class C has a compile error. The solution to this is like scenario 1, compile class C, and `generate increment CIL`. And again, no need to `generate full CIL`.

That’s it. Hope this article will help you, rather than waiting 4-5 hours doing nothing for the `full CIL generation` and ends up with same error as before. Try to check the modified class that the error class is used, they may contains error.

Steps to rules all of theses

In scenarios 1-2 we can knew the error class. However, in the case that we don’t know the error class. Following these steps to rule them all.

1. full compile AX  ( *** This is important, generate full CIL alone does not solve the problem )

2. fix all the compile error (  AX don’t allow us to deploy the new assembly if we had a compile error )

3. full generate CIL ( Some AX service group uses the CIL.NET assembly, so we have to generate CIL)

Once we can deploy the new .NET assembly, the problem is gone.