简体   繁体   中英

Loading assembly compiled at runtime into different AppDomain fails

I try to load a DLL which was compiled at runtime into a different AppDomain. This does not work while doing the same for system.dll works. This is my test code:

string sourceCode = "using System;\r\n" +
                     "[Serializable]\r\n" +
                     "public class Program1{\r\n" +
                     "   public static void Main1(){\r\n" +
                     "     int i = 100;\r\n" +
                     "     i++;" + 
                     "   }\r\n" +
                     "}";

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
Assembly[] assembliesOfCurrentDomain = AppDomain.CurrentDomain.GetAssemblies();//this.CompilerResults.CompiledAssembly.GetReferencedAssemblies();

for (int runAssembliesInCurrDomain = 0; runAssembliesInCurrDomain < assembliesOfCurrentDomain.Length; runAssembliesInCurrDomain++)
{
    try
    {
        parameters.ReferencedAssemblies.Add(assembliesOfCurrentDomain[runAssembliesInCurrDomain].Location);
    }
    catch
    {
    }
}

// True - memory generation, false - external file generation
parameters.GenerateInMemory = false;
parameters.OutputAssembly = "D:\\temp\\123.dll";
parameters.IncludeDebugInformation = true;
parameters.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location);

// True - exe file generation, false - dll file generation
parameters.GenerateExecutable = false;
parameters.TreatWarningsAsErrors = true;

CompilerResults results = provider.CompileAssemblyFromSource(parameters, sourceCode);

Assembly own = Assembly.LoadFrom("D:\\temp\\123.dll");
Assembly system = Assembly.LoadWithPartialName("System");

AppDomainSetup appDomainSetup = new AppDomainSetup()
{
    PrivateBinPath = @"D:\\temp"
};

AppDomain domain = AppDomain.CreateDomain("hello", AppDomain.CurrentDomain.Evidence, appDomainSetup);
domain.Load(system.GetName());               // works
AppDomain.CurrentDomain.Load(own.GetName()); // works
domain.Load(own.GetName());                  // works not

I get a FileNotFoundException with the following "FusionLog"

=== Zustandsinformationen vor Bindung ===
LOG: Benutzer = LIGHTTRANS2\schoening
LOG: DisplayName = 123, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/
LOG: Ursprünglicher PrivatePath = NULL
Aufruf von Assembly : (Unknown).
===
LOG: Diese Bindung startet im default-Load-Kontext.
LOG: Die Anwendungskonfigurationsdatei wird verwendet: D:\schoening\Projekte_VL\Testprojekte\Compileing\WindowsFormsApplication1\bin\x64\Debug\WindowsFormsApplication1.vshost.exe.config
LOG: Die Computerkonfigurationsdatei von C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config wird verwendet.
LOG: Die Richtlinie wird derzeit nicht auf den Verweis angewendet (private, benutzerdefinierte, teilweise oder pfadbasierte Assemblybindung)
LOG: Download von neuem URL file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/123.DLL.
LOG: Download von neuem URL file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/123/123.DLL.
LOG: Download von neuem URL file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/123.EXE.
LOG: Download von neuem URL file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/123/123.EXE.

Sorry that it is German, tomorrow I will try to post an English version. Any ideas what's the difference between the two assemblies?

The compiling of the DLL works. I have the same problem if I comment out everything before the Assembly own = Assembly.LoadFrom("D:\\\\temp\\\\123.dll") line and use the DLL compiled in the previous run.

Edit: Following a suggestion, I tried the following which does not work either.

 Assembly own = Assembly.LoadFrom("D:\\temp\\123.dll");

 AppDomainSetup appDomainSetup = new AppDomainSetup() {
     PrivateBinPath = @"D:\\temp"
 };

 //FileStream fs = own.GetFiles(true)[0]; // does not work either
 FileStream fs = new FileStream("D:\\temp\\123.dll", FileMode.Open, FileAccess.Read, FileShare.Read);
 byte[] rawAssembly = new byte[fs.Length];
 fs.Read(rawAssembly, 0, (int)fs.Length);

 AppDomain domain = AppDomain.CreateDomain("hello", AppDomain.CurrentDomain.Evidence, appDomainSetup);
 domain.Load(rawAssembly);  

The assembly name doesn't contain a full path to the assembly - the CLR doesn't have any way of finding your "temp" assembly. If you want to load a specific assembly file into an AppDomain , you'll have to use the byte[] overload.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM