C# 从AppDomain解析InvalidOperationException
我使用的插件运行在AppDomains基本上与此代码C# 从AppDomain解析InvalidOperationException,c#,plugins,appdomain,C#,Plugins,Appdomain,我使用的插件运行在AppDomains基本上与此代码 AppDomain appDomain = AppDomain.CreateDomain("MyDomain"); PluginLoader loader = appDomain.CreateInstanceFromAndUnwrap( Assembly.GetExecutingAssembly().Location, typeof(PluginLoader).FullName); loader.LoadPlugIns(); AppD
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又会加载相同的程序集文件。我看不出有什么原因会导致问题
但是,在我的情况下,会不时抛出带有此堆栈跟踪的invalidoOperationException
:
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,但没有成功。为了防止其他人遇到同样的问题,像这样创建AppDomain似乎可以解决问题
Evidence evidence = AppDomain.CurrentDomain.Evidence
AppDomain appDomain = AppDomain.CreateDomain("MyDomain", evidence);