我發現了很多類似的問題,但找不到任何解決方案。引發AppDomain異常
我有以下代碼:
string file = "c:\\abc.dll";
AppDomainSetup ads = new AppDomainSetup();
ads.PrivateBinPath = Path.GetDirectoryName(file);
AppDomain ad2 = AppDomain.CreateDomain("AD2", null, ads);
ProxyDomain proxy = (ProxyDomain)ad2.CreateInstanceAndUnwrap(typeof(ProxyDomain).Assembly.FullName, typeof(ProxyDomain).FullName);
Assembly asm = proxy.GetAssembly(file); // exception File.IO assembly not found is thrown here after succesfully running the funktion GetAssembly.
public class ProxyDomain : MarshalByRefObject
{
public Assembly GetAssembly(string assemblyPath)
{
try
{
Assembly asm = Assembly.LoadFile(assemblyPath);
//...asm is initialized correctly, I can see everything with debugger
return asm;
}
catch
{
return null;
}
}
}
是那麼我GetAssembly的Funktion返回一些其他類型的,甚至是我的自定義序列化類最有趣的事情,一切都很好。有人知道我錯過了什麼嗎?或者只是不可能將加載的程序集返回到另一個域?
謝謝
你爲什麼試圖返回一個'Assembly'到父應用程序域?通常,這將被避免,所以父應用程序域不必加載程序集,並且可以在卸載子應用程序域時卸載它。 –
如果你用這個ProxyDomain類來解釋你想要達到的目標,這將有所幫助。爲什麼你需要一個新的AppDomain如果你要返回完整的程序集到你的主域? –
那麼,我真的不需要在我的第一個應用程序域中的這個程序集。我在文件夾中有很多它們,我必須找到包含特殊類屬性的程序集並返回一個dll名稱列表。我需要第二個應用程序域,因爲在應用程序重新啓動之前,無法刪除在主AppDomain中加載的所有程序集。我解決了我的問題,但仍然知道爲什麼它會返回所有其他類型,並且無法返回Assembly。 – VladL