Extensions: Unable to resolve services when using ServiceProvider and AssemblyDependencyResolver together

Created on 3 Dec 2019  路  1Comment  路  Source: dotnet/extensions

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.

question

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:

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

>All comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mvrmoreira picture mvrmoreira  路  3Comments

SnakyBeaky picture SnakyBeaky  路  6Comments

christiannagel picture christiannagel  路  4Comments

JunTaoLuo picture JunTaoLuo  路  4Comments

shirhatti picture shirhatti  路  6Comments