簡體   English   中英

將運行時編譯的程序集加載到不同的AppDomain中失敗

[英]Loading assembly compiled at runtime into different AppDomain fails

我嘗試將在運行時編譯的DLL加載到其他AppDomain中。 為system.dll進行相同操作時,這將不起作用。 這是我的測試代碼:

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

我收到帶有以下“ FusionLog”的FileNotFoundException

=== 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.

抱歉,它是德語,明天我將嘗試發布英語版本。 任何想法這兩個程序集之間有什么區別?

DLL的編譯工作。 如果我注釋掉Assembly own = Assembly.LoadFrom("D:\\\\temp\\\\123.dll")行之前的所有內容,並使用上次運行中編譯的DLL, Assembly own = Assembly.LoadFrom("D:\\\\temp\\\\123.dll")相同的問題。

編輯:根據建議,我嘗試了以下兩種方法也不起作用。

 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);  

程序集名稱不包含該程序集的完整路徑-CLR無法找到“臨時”程序集。 如果要將特定的程序集文件加載到AppDomain ,則必須使用byte[]重載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM