CSharpCodeProvider.CompileAssemblyFromSource Fails Due To mscorlib.dll could not be opened

  • Thread starter Thread starter Lior Simon
  • Start date Start date
L

Lior Simon

Guest
Hello,

My application generates several assemblies from source codes on the fly.

Every once in a while the application generates the assemblies again using new source codes.

Sometimes, the generation of the assembly fails with the following compilation error:

"Metadata file 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll' could not be opened -- 'The process cannot access the file because it is being used by another process.'. Line:0".

Here is my code that generates an assembly:

public CompilerResults CompileAssymbly(
string sourceCode,
string[] assemblyNamesToReference)
{
var codeProviderParameters = new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } };
using (var codeProvider = new CSharpCodeProvider(codeProviderParameters))
{
var compilerParameters = new CompilerParameters();
compilerParameters.GenerateInMemory = true;
compilerParameters.GenerateExecutable = false;
compilerParameters.IncludeDebugInformation = false;
compilerParameters.CompilerOptions = "/nowarn:612,618"; //Suppress compiler errors caused by the Obsolete attribute
compilerParameters.WarningLevel = 4;
if (assemblyNamesToReference != null)
compilerParameters.ReferencedAssemblies.AddRange(assemblyNamesToReference);

return
codeProvider.CompileAssemblyFromSource(compilerParameters, sourceCode);

}
}


How come mscorlib.dll is locked for reading?

Why isn't the compilation result deterministic, in the sense that usually it succeeds while only sometimes it fails?

How can I resolve the issue?


Thanks

Continue reading...
 
Back
Top