I have created a sample project:
https://github.com/Matthew-Bonner/ProblemActivatingPlugins
When you run it, you will get the exception:
System.InvalidOperationException: 'Unable to resolve service for type 'Microsoft.Extensions.Logging.ILoggerFactory' while attempting to activate 'ExamplePlugin.Plugin'.'
I'm unsure as to why the service can't be resolved, have even gone as far as copying down the classes and debugging line by line.
In a very crude way, the type ILoggerFactory in the ServiceProvider does not match the ILoggerFactory type defined in the constructor, despite both referencing the same project that contains the ILoggerFactory type, and all projects being built at the same time.
When using load context you need to think about the assemblies/types as being identified by their load context instance and their fully qualified type name. So for example, your project has the DefaultLoadContext and a PluginLoadContext. All types loaded into the plugin load context you can think of as being prefixed by that PluginLoadContext instance:
PluginLoadContext.IPlugin
PluginLoadContext.ExamplePlugin
PluginLoadContext.ILogger
DefaultLoadContext.IPlugin
DefaultLoadContext.ILogger
You're loading the IPlugin and ILogger types into the PluginLoadContext and then trying to reference them as DefaultLoadContext.IPlugin and PluginLoadContext.ILogger.
What you really want, is to have ILogger and IPlugin come from the host and have ExamplePlugin come from the PluginLoadContext:
PluginLoadContext.ExamplePlugin
DefaultLoadContext.IPlugin
DefaultLoadContext.ILogger
Here's the fix: https://github.com/Matthew-Bonner/ProblemActivatingPlugins/pull/1/files
Also, check out this package: https://github.com/natemcmaster/DotNetCorePlugins
Most helpful comment
When using load context you need to think about the assemblies/types as being identified by their load context instance and their fully qualified type name. So for example, your project has the DefaultLoadContext and a PluginLoadContext. All types loaded into the plugin load context you can think of as being prefixed by that PluginLoadContext instance:
You're loading the IPlugin and ILogger types into the PluginLoadContext and then trying to reference them as DefaultLoadContext.IPlugin and PluginLoadContext.ILogger.
What you really want, is to have ILogger and IPlugin come from the host and have ExamplePlugin come from the PluginLoadContext:
Here's the fix: https://github.com/Matthew-Bonner/ProblemActivatingPlugins/pull/1/files
Also, check out this package: https://github.com/natemcmaster/DotNetCorePlugins