My program often compile c#-code and sometimes i got a ArgumentException like "The file name 'C:\Users--\AppData\Local\Temp\wvpc3m5m.0.cs' was already in the collection. Parameter name: fileName".
Settings of compiler is next:
public void Init()
{
this.compilerParameters = new CompilerParameters
{
GenerateExecutable = false,
GenerateInMemory = true
};
}
Compilation:
public CompilerResults Compile(String code)
{
CompilerResults result = this.codeProvider
.CompileAssemblyFromSource(this.compilerParameters, code);
return result;
}
I think that the codeProvider write passed string into a file, and sometimes he try to write in one and the same file twice.
The problem was that all CSharpCodeProvider's instance can compile the code once a time slot. This code solve problem:
private readonly Object lockCompile = new Object();
public CompilerResults Compile(String code)
{
CompilerResults result = null;
lock (lockCompile)
{
using (CSharpCodeProvider codeProvider = new CSharpCodeProvider())
{
result = codeProvider
.CompileAssemblyFromSource(this.compilerParameters, code);
}
}
return result;
}