Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can this library work as Plugin Loader? #3

Closed
aloksharma1 opened this issue Apr 4, 2022 · 3 comments
Closed

Can this library work as Plugin Loader? #3

aloksharma1 opened this issue Apr 4, 2022 · 3 comments
Labels
question Further information is requested

Comments

@aloksharma1
Copy link

aloksharma1 commented Apr 4, 2022

Hello,

Instead of Modules i want my assembly subparts to behave as plugins, load/unload on runtime without affecting host app. Can you guide me how this is possible with this library, currently i am using DotnetCorePlugins
but this have bug in razor that your library solves, so i am interested if i can port to yours.

thanks

@John0King
Copy link
Owner

John0King commented Apr 5, 2022

Can this library work as Plugin Loader

yes, but you may want to use DotnetCorePlugins because this project is in slow development and for reseach purpose currently (no officol release on nuget.org)

but this have bug in razor that your library solves

well, there are only two ways to acrchve plugins, 1). Assembly.Load and use MemeoryStream or byte[] 2) AssemblyLoadContext aka ALC. the problem that razor happed is that the razor is load to Default ALC in "hard code" way (ps: .net 6 + Source Generator should fixed this because now the razor files are generat .g.cs in your project obj folder and there are no additional assembly need to be load).
and (for below .net 6 ) this project fix it in a very simple way , it add a fallback method to the default ALC and forward it to the plugin ALC.

// razor bug fix
AssemblyLoadContext.Default.Resolving += (alc, asbn) =>
{
return AssemblyLoadContext.All.OfType<PluginAssemblyLoadContext>()
.Where(x => x.PluginInfo.PluginName.Equals(asbn.Name, StringComparison.OrdinalIgnoreCase))
.FirstOrDefault()?.LoadFromAssemblyName(asbn);
//if (AssemblyLoadContext.CurrentContextualReflectionContext == null)
//{
// return null;
//}
//if (AssemblyLoadContext.CurrentContextualReflectionContext == alc)
//{
// return null;
//}
//if (dic.TryGetValue(asbn.FullName, out var c))
//{
// if (c > 0)
// {
// return null;
// }
//}
//if (dic.ContainsKey(asbn.FullName))
//{
// dic[asbn.FullName] = dic[asbn.FullName] + 1;
//}
//else
//{
// dic[asbn.FullName] = 0;
//}
//return AssemblyLoadContext.CurrentContextualReflectionContext.LoadFromAssemblyName(asbn);
};
app.UseSubAppModules("/m");

and that means you can not make a razor dll's assembly name as same as other plugins (because the way that razor add to Default ALC is not compatiable with the "Plugin architecture" )

@John0King
Copy link
Owner

be notice that unloadable ALC have many limit as #1 meation ,
for example :

and thats why this project no active develop.

@aloksharma1
Copy link
Author

ok, so i will try to do this in net 6 but i am also ready to wait till net 7 if that's going to solve these issues that you have pointed out.
i am very careful about point 2 as all development follow one common project as reference assembly holder+host & plugin folders only use specific refernces when/if required and not available in main project.

@John0King John0King added the question Further information is requested label Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants