weikio / PluginFramework

Everything is a Plugin in .NET
MIT License
538 stars 101 forks source link

Question/observation: necessity to keep catalog reference #55

Open unwork-ag opened 2 years ago

unwork-ag commented 2 years ago

Hi - I just ran across an issue which I think understand now: I had the problem that I could successfully instantiate plugins, but for some reason could not invoke a method on one of them because that required to load an additional assembly. At this time the AssemblyLoadContext was in state Unloading and I got this exception:

System.IO.FileLoadException: Could not load file or assembly 'MyPluginLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. An operation is not legal in the current state. (0x80131509) File name: 'MyPluginLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' ---> System.InvalidOperationException: AssemblyLoadContext is unloading or was already unloaded. at System.Runtime.Loader.AssemblyLoadContext.VerifyIsAlive() at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String assemblyPath) at Weikio.PluginFramework.Context.PluginAssemblyLoadContext.Load(AssemblyName assemblyName) at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName) at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName)

At this time I no longer had a reference to the plugin catalog which itself holds a reference to the AssemblyLoadContext. As soon as I kept the reference to the catalog this issue was gone.

Is it correct that I have to keep a permanent reference to the catalog to ensure that delayed loading of dependencies can be done for a plugin?