Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOException when dynamically compiling code

I have the following line of code:

CSharpCodeProvider c = new CSharpCodeProvider();
CompilerParameters cp = new CompilerParameters();
cp.ReferencedAssemblies.Add("system.dll");
cp.CompilerOptions = "/t:library";
cp.GenerateInMemory = true;
CompilerResults cr = c.CompileAssemblyFromSource(cp, sb.ToString());

Which results in an IOException being thrown.

Additional information: The process cannot access the file 'C:\Users\Username\AppData\Local\Temp\dgl5fb1i.err' because it is being used by another process.

However, this is a part of a large program that consumes more than 8GB of RAM. On a system with 16GB of RAM, this exception is not thrown. The code that is dynamically compiled compiles well and runs. The program runs without any errors on a system with sufficient RAM. The program is compiled for x64. Note that I am not getting OutOfMemoryException or any indication that the program is out of memory. In the Task Manager the memory usage almost reaches the top before the IOException is thrown.

What could be causing this behavior and can anyone suggest a solution?

EDIT

I modified the application to use substantially less memory. The error persists even though the application has enough available memory. The issue still occurs only on one machine.

This could be related to the following post: Prevent CompileAssemblyFromSource from generate temp files with duplicate file name.

like image 791
Igor Ševo Avatar asked May 05 '16 10:05

Igor Ševo


1 Answers

I was running several compilation in parallel. It was necessary to change the temporary directory for each compilation, so that conflicts do not occur.

CSharpCodeProvider prov = new CSharpCodeProvider();
CompilerParameters parms = new CompilerParameters();
parms.TempFiles = new TempFileCollection(tempdir);
like image 53
Igor Ševo Avatar answered Nov 02 '22 16:11

Igor Ševo