I have the following code running in a Windows Service that's been working for years without problems even under heavy concurrency:
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
parameters.OutputAssembly = outputAssemblyFile;
CompilerResults results = codeProvider.CompileAssemblyFromFile(parameters, "file.cs");
if (results.Errors.Count > 0)
{
Console.WriteLine("Compile ERROR");
}
else
{
Console.WriteLine("Compile OK");
Console.WriteLine("Assembly Path:" + results.PathToAssembly);
Console.WriteLine("Assembly Name:" + results.CompiledAssembly.FullName);
}
Usually, when the code is successfully compiled, since parameters.GenerateInMemory is set to true, the results.PathToAssembly is null (as specified in the MSDN)
One of these days I started having an issue where sometimes the code was being successfully compiled but results.PathToAssembly was NOT null. Additionally, CompiledAssembly was returning a FileNotFoundException but when I checked the path indicated by results.PathToAssembly the assembly was actually there. I could not, however, make sure it was there at that specific moment.
I restarted the service and everything was back on track.
Is there any obvious reason for this to happen?
Thinking this could be some permission issue I tried to reproduce the issue by having the assembly already created and setting the file read-only, but that caused the compilation to fail.
parameters.OutputAssemblyifparameters.GenerateExecutable == false?