question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Why is not the given default context resolving the type if PreferSharedTypes is set to true

See original GitHub issue

Hi,

first of all thank you for your effort on this project. This is a really nice project and I try to adapt it for our plugin system.

Since I need plugins that can have a reference to some other plugins I looked at this issue #60

I encountered some problems by trying the two suggested solutions and wanted to try another way.

Starting with my Idea I found that this following lines will not resolve the type defined in my created AssemblyLoadContext, but the type defined in the Assembly instance loaded by the PluginLoader.

string? pluginName = Path.GetFileName(pluginDirectory);
string assemblyFile = Path.Combine(pluginDirectory, pluginName + ".dll");

AssemblyLoadContext pluginAssemblyLoadContext = new AssemblyLoadContext(pluginName);
pluginAssemblyLoadContext.LoadFromAssemblyPath(assemblyFile);

PluginLoader loader = PluginLoader.CreateFromAssemblyFile(
    assemblyFile: assemblyFile,
    true,
    sharedTypes: Array.Empty<Type>(),
    config =>
    {
        config.DefaultContext = pluginAssemblyLoadContext;
        config.PreferSharedTypes = true;
    });

Assembly pluginAssembly = loader.LoadDefaultAssembly();
Type[] allTypes = pluginAssembly.GetTypes();

IEnumerable<Type> pluginTypes = allTypes.Where(t => typeof(IPlugin).IsAssignableFrom(t) && !t.IsAbstract);

Why is this? I’m not sure if I found all the available documentation about the ALC and this PluginLoader lib stuff, so maybe I just didn’t understand all that ALC fallback things right. My assumption was that it should be possible to build a tree of ALCs that has the default ALC (implicitly) as root.

I thought here https://github.com/natemcmaster/DotNetCorePlugins/blob/8312d70c90801f6caf3f39b6e1341d26689c5111/src/Plugins/Loader/ManagedLoadContext.cs#L100 this “routing” is done - but it seems that I missed something.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
natemcmastercommented, Feb 23, 2020

Thanks for the updated diagrams and explanation. It was useful to learn from your example. I’m not planning on making changes to this library at the moment. I think your code is a good example of how to use AssemblyLoadContext if you have scenarios that extend beyond the current capability of this library, but I don’t see enough need for this right now to add anything to the library. I’ll leave this issue open for additional discussion. If there is more demand for something like this in the future, I’ll reconsider, but for now, it doesn’t appear to warrant the effort.

Thanks, Nate

0reactions
stale[bot]commented, Jul 21, 2021

Closing because there was no response to the previous comment. If you are looking at this issue in the future and think it should be reopened, please make a comment and mention natemcmaster so he sees it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does the compiler fail to find the right type?
In the first case the compiler knows you mean the class because of the new keyword. What follows new has to be a...
Read more >
Solved In the context of C++ and default arguments.
Question : If a non-default argument is specified at the Blank 2 of the argument listing then the system will not be able...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found