C# 加载然后卸载带有依赖项的DLL会锁定DLL
动态加载带有依赖项的DLL,然后卸载它,仍然会锁定该DLL,并且我无法删除/替换该DLL 作为编写插件应用程序的一部分,我正在动态加载DLL(它具有依赖项,例如Newtonsoft.Json),运行加载的程序集,然后卸载它。卸载后,我无法从磁盘删除DLL(直到我重新启动应用程序),但是,如果我使用没有依赖项的DLL,它工作正常,并且不会锁定文件。 该实现基于.NET core 3加载/卸载,该加载/卸载来自: 我使用带有解析器的C# 加载然后卸载带有依赖项的DLL会锁定DLL,c#,.net,.net-core,dynamic-loading,.net-core-3.0,C#,.net,.net Core,Dynamic Loading,.net Core 3.0,动态加载带有依赖项的DLL,然后卸载它,仍然会锁定该DLL,并且我无法删除/替换该DLL 作为编写插件应用程序的一部分,我正在动态加载DLL(它具有依赖项,例如Newtonsoft.Json),运行加载的程序集,然后卸载它。卸载后,我无法从磁盘删除DLL(直到我重新启动应用程序),但是,如果我使用没有依赖项的DLL,它工作正常,并且不会锁定文件。 该实现基于.NET core 3加载/卸载,该加载/卸载来自: 我使用带有解析器的AssemblyLoadContext,例如: class Tes
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);
....
....
}
问题基本上解决了,当卸载DLL时,如果你有一个Newtonsoft.Json依赖项,你就不能这样做,因为他们有一个锁定文件的bug
这是基于我打开的响应解决的问题,基本上在卸载DLL时,如果你有Newtonsoft.Json依赖项,你就不能这样做,因为他们有一个锁定文件的bug
这是基于我打开的回复您对此问题有任何更新吗?因为我也在寻找解决问题的办法this@Justin99b我回答了我自己的问题,这是给你和所有其他人的:)你对这个问题有任何更新吗?因为我也在寻找解决问题的办法this@Justin99b我回答了自己的问题,这是为了你和所有其他人:)