动态加载具有依赖项的DLL,然后将其卸载,仍然锁定DLL,而我无法删除/替换该DLL。
在编写插件应用程序的过程中,我会动态加载DLL(具有相关性,例如Newtonsoft.Json),运行加载的程序集,然后将其卸载。卸载后,我无法从磁盘上删除DLL(直到重新启动应用程序为止),但是,如果我使用没有依赖性的DLL,它可以正常工作,并且不锁定文件。 该实现基于.NET core 3的加载/卸载,取自: https://docs.microsoft.com/en-us/dotnet/standard/assembly/unloadability
我使用的是带有解析器的AssemblyLoadContext
,例如:
class TestAssemblyLoadContext : AssemblyLoadContext
{
private AssemblyDependencyResolver _resolver;
public TestAssemblyLoadContext(string mainAssemblyToLoadPath) : base(isCollectible: true)
{
_resolver = new AssemblyDependencyResolver(mainAssemblyToLoadPath);
}
protected override Assembly Load(AssemblyName name)
{
string assemblyPath = _resolver.ResolveAssemblyToPath(name);
if (assemblyPath != null)
{
return LoadFromAssemblyPath(assemblyPath);
}
return null;
}
}
和创建上下文的代码:
[MethodImpl(MethodImplOptions.NoInlining)]
public static void runCommands(string pluginPath, bool execute,out WeakReference alcWeakRef)
{
string pluginLocation = getPath(pluginPath);
PluginLoadContext loadContext = new PluginLoadContext(pluginLocation);
alcWeakRef = new WeakReference(loadContext, trackResurrection: true);
Assembly pluginAssembly = loadContext.LoadFromAssemblyName(new AssemblyName(Path.GetFileNameWithoutExtension(pluginLocation)));
var commands = CreateCommands(pluginAssembly).ToList();
if (execute) {
Console.WriteLine("Commands: ");
foreach (ICommand command in commands)
{
Console.WriteLine($"executing... {command.Execute()}");
}
}
commands.Clear();
loadContext.Unload();
}
如果这是我做错的事情,我会徘徊,我已经尝试从流中加载文件,例如:
using (var fs = new FileStream(pluginLocation, FileMode.Open, FileAccess.Read))
{
var pluginAssembly = loadContext.LoadFromStream(fs);
....
....
}
答案 0 :(得分:0)
问题已解决,基本上,在卸载DLL时,如果您具有Newtonsoft.Json依赖项,则不能这样做,因为它们具有锁定文件的错误。
这是基于我打开的github issue的回复