1
votes

I am using plugins running in AppDomains basically with this code

AppDomain appDomain = AppDomain.CreateDomain("MyDomain");
PluginLoader loader = appDomain.CreateInstanceFromAndUnwrap(
    Assembly.GetExecutingAssembly().Location, typeof(PluginLoader).FullName);
loader.LoadPlugIns();
AppDomain.Unload(appDomain);

LoadPlugIns loads and runs the plugin assemblies using Assembly.LoadFrom().

This seems like a typical design pattern that's well documented (e.g. How to Load an Assembly to AppDomain with all references recursively?). The one exception may be that multiple AppDomains may be loaded concurrently which in turn load the same assembly files. I don't see any reason though why this should be causing problems.

However, in my case from time to time an InvalidOperationException is thrown with this stack trace:

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

I am struggling to understand why this is happening and how it can be fixed. It seems the exception is not triggered from my code. So there is no chance to catch it in a try/catch block. I tried registering the AppDomain as suggested in Unregister Lease throws InvalidOperationException to no avail.

1

1 Answers

2
votes

Just in case anyone else is experiencing the same issue, creating the AppDomain like this seems to resolve it.

Evidence evidence = AppDomain.CurrentDomain.Evidence
AppDomain appDomain = AppDomain.CreateDomain("MyDomain", evidence);