Type identity mismatches in plugin framework when plugins depend on one another
In a plugin framework, suppose that one plugin has a dependency on another.
As per https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support, I've given every plugin its own
AssemblyLoadContext
, while the shared 'core' library is loaded into AssemblyLoadContext.Default
. This has worked fine to avoid type identity mismatches on types of the core library. However, I've run into a different source of issues.
I instantiate plugins as follows:
then store them in a class in the shared core library that exposes a method:
The end result is that a plugin that is a dependency of another plugin is loaded, effectively, twice:
- the first time in its own PluginLoadContext
- a second time as a dependency of another plugin, in that plugin's PluginLoadContext
and thus get_plugin
routinely fails with a type identity mismatch.
How does one properly handle this situation? The intention is that:
- plugins are instantiated once by the 'core'
- those instances are retrieved at runtime by other plugins, if need be1 Reply
I see. When probing you can return a reference to an
Assembly
loaded in a different AssemblyLoadContext
and that triggers sharing.
This solves the issue.