-
-
Notifications
You must be signed in to change notification settings - Fork 227
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
Make plugins unloadable #16
Comments
Should be pretty straightforward to implement as well as it seem: sample. Only 3.0 need to become available. 😎 |
Thank you for the info. However, after unloading, the DLL could not be deleted from the directory? Have I possibly ignored something? Thanks in advance. Code: class SimpleUnloadableAssemblyLoadContext : AssemblyLoadContext
{
public SimpleUnloadableAssemblyLoadContext()
: base(isCollectible: true)
{
}
protected override Assembly Load(AssemblyName assemblyName) => null;
}
class Program
{
private static void ExecuteAssembly(Assembly assembly)
{
MethodInfo entry = assembly.EntryPoint;
foreach (Type type in assembly.GetTypes())
{
Console.WriteLine(type.FullName);
}
Console.WriteLine("ok");
}
static void Main(string[] args)
{
AssemblyLoadContext tt = new SimpleUnloadableAssemblyLoadContext();
tt.LoadFromAssemblyPath(@"C:\Lokale Daten\AppDomainUnload\ConsoleApp1\bin\Debug\netcoreapp3.0\Plugin\Plugin1.dll");
tt.Unload();
Console.ReadKey();
}
private static void Context_Unloading(AssemblyLoadContext obj)
{
Console.WriteLine("Unloading");
}
} |
If you are not able to delete a dll after unloading its load context, it is probably a bug in .NET Core 3. Checkout https://github.com/dotnet/coreclr/projects/9 to see if this is a known issue. If not, open a new bug on dotnet/coreclr. |
Based on .NET Core 3 preview 1 Is this issue possible to solve now or need the next versions? |
Yes. 3.0 preview 1 should have AssemblyLoadContext.Unload in it. I don't think there is any use for MetadataLoadContext in this project. That API is more about "reflection-only" loading. It's similar to AssemblyLoadContext, but not exactly the same. |
Initial implementation #31. UsageUnloadable plugins is supported in .NET Core 3.0 Preview 2 and newer. To unload a plugin, dispose the loader. var loader = PluginLoader.CreateFromAssemblyFile(
pluginDll,
sharedTypes: new [] { typeof(IPlugin) });
// do stuff
loader.Dispose(); // this triggers an unload |
Any ideas whether this would allow asp.net core application parts to be unloaded (then reloaded from new assemblies) ? I asked / raised an issue but the response was its not on asp.net core roadmap: dotnet/aspnetcore#7219 - I wonder if you have had any thoughts around this? |
It might. Unloading is very new and I haven't played with it enough to try this out. Calling |
Merged #31 which adds API when using .NET Core 3.0 to allow unloading. UsageUnloadable plugins is supported in .NET Core 3.0 Preview 2 and newer. To unload a plugin, make sure isUnloadable = var loader = PluginLoader.CreateFromAssemblyFile(
pluginDll,
sharedTypes: new [] { typeof(IPlugin) },
isUnloadable: true);
// do stuff
loader.Dispose(); // this triggers an unload In order for this to work, you must make sure you have not left behind any variables (static, local, etc) or manually allocated memory. Once GC has collected everything, the assembly will be unloaded. See dotnet/coreclr#22221 |
Cool stuff. Is there a way to tell if a particular assembly has been unloaded? For example some kind of log output or something? |
There are some events triggered and you can also use a debugger. See https://docs.microsoft.com/en-us/dotnet/standard/assembly/unloadability-howto |
This will require .NET Core 3.0 and up, which has added libraries to make assembly load contexts unloadable.
See https://github.com/dotnet/coreclr/issues/552, dotnet/coreclr#18476, https://github.com/dotnet/corefx/issues/14724, and https://github.com/dotnet/coreclr/projects/9.
Update
Merged #31 which adds API when using .NET Core 3.0 to allow unloading.
Usage
Unloadable plugins is supported in .NET Core 3.0 Preview 2 and newer. To unload a plugin, make sure isUnloadable =
true
and dispose the loader to trigger the unload.In order for this to work, you must make sure you have not left behind any variables (static, local, etc) or manually allocated memory. Once GC has collected everything, the assembly will be unloaded. See dotnet/coreclr#22221
The text was updated successfully, but these errors were encountered: