Insufficient Memory, System.IO.File::ReadAllText


I developed an AIF service to import trade agreement journals. This works great but the end users occasionally request to import extremely large files. Upwards of 80,000 records into the PriceDiscAdmTrans table at one time. So far it has always worked great, but today we attempted an XML file that was 26,666 KB. This threw an error: Insufficient memory to run script. After much research, I discovered that this is being generated from the AIF gateway prior to actually importing the file. In the AIFGatewayQueue table method, AX attempts to assign the entire XML to a string variable using System.IO.File::ReadAllText. This works great up to 25MB. Once the XML file is one byte over 25MB, it throws the error. The AX string isn’t the issue I believe. It really seems to be the System.IO.File manipulation. It can’t exceed 25MB.

So, I’m wondering if there is any way to modify the size limit imposed on the System.IO.File::ReadAllText. Or, if that is not possible, what would be the best way to read the XML (txt file in reality) in sections and amend it to the string. Any info or suggestions would be helpful. Thanks!

Your assumption is correct; System.IO.File surely can handle even much larger files. “Insufficient memory to run script” is thrown by AX, not by the .NET class. If it was the case, you would get an CLR exception instead.

Please read my blog post about the problem. It explains the mechanism of the issue and how you can change the maxbuffersize parameter. Nevertheless you can’t increase the parameter infinitely, so it doesn’t scale up. A better approach would be simply splitting your file if it’s too large.