[英]Resolving InvalidOperationException from AppDomain
我基本上是通過此代碼使用在AppDomains中運行的插件
AppDomain appDomain = AppDomain.CreateDomain("MyDomain");
PluginLoader loader = appDomain.CreateInstanceFromAndUnwrap(
Assembly.GetExecutingAssembly().Location, typeof(PluginLoader).FullName);
loader.LoadPlugIns();
AppDomain.Unload(appDomain);
LoadPlugIns
使用Assembly.LoadFrom()
加載並運行插件程序集。
這似乎是一個典型的設計模式,已被充分記錄(例如, 如何使用所有引用遞歸地將程序集加載到AppDomain? )。 一個例外可能是可以同時加載多個AppDomain,從而依次加載相同的程序集文件。 我看不出有什么原因會引起問題。
但是,在我的情況下,此堆棧跟蹤有時會拋出InvalidOperationException
:
System.InvalidOperationException: Handle is not initialized.
at System.WeakReference.set_Target(Object value)
at System.Runtime.Remoting.IdentityHolder.SetIdentity(Identity idObj, String URI, DuplicateIdentityOption duplicateOption)
at System.Runtime.Remoting.IdentityHolder.FindOrCreateIdentity(String objURI, String URL, ObjRef objectRef)
at System.Runtime.Remoting.RemotingServices.InternalUnmarshal(ObjRef objectRef, Object proxy, Boolean fRefine)
at System.Runtime.Remoting.RemotingServices.CreateProxyForDomain(Int32 appDomainId, IntPtr defCtxID)
at System.AppDomain.GetDefaultDomain()
at System.AppDomain.get_EvidenceNoDemand()
at System.AppDomain.get_Evidence()
at System.IO.IsolatedStorage.IsolatedStorage.InitStore(IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
at System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(IsolatedStorageScope scope, Type domainEvidenceType, Type assemblyEvidenceType)
at MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder.Dispose(Boolean disposing)
at MS.Internal.IO.Packaging.PackagingUtilities.ReliableIsolatedStorageFileFolder.Finalize()
我正在努力了解為什么會發生這種情況以及如何解決它。 看來異常不是由我的代碼觸發的。 因此,沒有機會在try
/ catch
塊中捕獲它。 我嘗試按照取消注冊租約中的建議注冊AppDomain,導致InvalidOperationException無效。
以防萬一其他人遇到相同的問題,像這樣創建AppDomain似乎可以解決它。
Evidence evidence = AppDomain.CurrentDomain.Evidence
AppDomain appDomain = AppDomain.CreateDomain("MyDomain", evidence);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.