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

  • Question

  • 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)
                        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?


    Tuesday, December 3, 2019 9:14 AM

All replies

  • If it happens when you call CompileAssembly multiple times with no interval between operations, maybe you should retry the failed operations, as a workaround.

    Tuesday, December 3, 2019 10:50 AM
  • Thanks Viorel_, 

    I would like to get to the bottom of this issue.


    Tuesday, December 3, 2019 12:20 PM
  • I've not seen or heard of this problem before. Could be a bug in the CodeDOM. Are you compiling across threads? What CLR version? 

    Michael Taylor

    Tuesday, December 3, 2019 3:01 PM
  • The compiling is done in one thread. The process has other threads as well that do other things unrelated to this assembly generation.

    The .Net version is .Net 4.6.1 though we've seen this issue for many years, even in .Net4.0 and maybe even in .Net3.5 though I'm not sure about the last one.


    Wednesday, December 4, 2019 12:31 PM
  • My gut instinct is that it is a known bug in the CodeDOM. Have you tried using the newer CSharpCodeProvider based upon Roslyn to see if the errors go away? They may have already resolved the issue. To use this you'd need to pull in a NuGet package but ASP.NET does it this way and it is constantly compiling stuff. I'm not aware of any issues with it.

    Michael Taylor

    Wednesday, December 4, 2019 3:14 PM
  • Hi Lior Simon,

    Thank you for posting here.

    I used the code you provided for many tests, but did not encounter any errors.

    I guess this error may be caused by your other code.

    Please check your source code or provide more information for us to further test .

    The sourceCode I used:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Resources;
    namespace ConsoleApp1
        public class Student
            public static void Main()
                string str = Console.ReadLine();
                using (ResXResourceWriter resx = new ResXResourceWriter(@".\CarResources.resx"))
                    resx.AddResource("Title", "Classic American Cars");
                    resx.AddResource("Name", "test1");
                    resx.AddResource("Age", "22");
                Dictionary<string, string> pairs = new Dictionary<string, string>();
                using (ResXResourceReader resxReader = new ResXResourceReader(@".\CarResources.resx"))
                    foreach (DictionaryEntry entry in resxReader)
                        pairs.Add(entry.Key.ToString(), entry.Value.ToString());

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, December 5, 2019 2:59 AM