.Net core 的熱插拔機(jī)制的深入探索,以及卸載問題求救指南.一.依賴文件*.deps.json的讀取.依賴文件內(nèi)容如下.一般位于編譯生成目錄中{ "runtimeTarget": { "name": ".NETCoreApp,Version=v3.1", "signature": "" }, "compilationOptions": {}, "targets": { ".NETCoreApp,Version=v3.1": { "PluginSample/1.0.0": { "dependencies": { "Microsoft.Extensions.Hosting.Abstractions": "5.0.0-rc.2.20475.5" }, "runtime": { "PluginSample.dll": {} } }, "Microsoft.Extensions.Configuration.Abstractions/5.0.0-rc.2.20475.5": { "dependencies": { "Microsoft.Extensions.Primitives": "5.0.0-rc.2.20475.5" }, "runtime": { "lib/netstandard2.0/Microsoft.Extensions.Configuration.Abstractions.dll": { "assemblyVersion": "5.0.0.0", "fileVersion": "5.0.20.47505" } } ...
使用DependencyContextJsonReader加載依賴配置文件源碼查看using (var dependencyFileStream = File.OpenRead("Sample.deps.json")) { using (DependencyContextJsonReader dependencyContextJsonReader = new DependencyContextJsonReader()) { //得到對(duì)應(yīng)的實(shí)體文件 var dependencyContext = dependencyContextJsonReader.Read(dependencyFileStream); //定義的運(yùn)行環(huán)境,沒有,則為全平臺(tái)運(yùn)行. string currentRuntimeIdentifier= dependencyContext.Target.Runtime; //運(yùn)行時(shí)所需要的dll文件 var assemblyNames= dependencyContext.RuntimeLibraries; } }
二.Net core多平臺(tái)下RID(RuntimeIdentifier)的定義.安裝 Microsoft.NETCore.Platforms包,并找到runtime.json運(yùn)行時(shí)定義文件.{ "runtimes": { "win-arm64": { "#import": [ "win" ] }, "win-arm64-aot": { "#import": [ "win-aot", "win-arm64" ] }, "win-x64": { "#import": [ "win" ] }, "win-x64-aot": { "#import": [ "win-aot", "win-x64" ] }, }
NET Core RID依賴關(guān)系示意圖win7-x64 win7-x86 | \ / | | win7 | | | | win-x64 | win-x86 \ | / win | any
.Net core常用發(fā)布平臺(tái)RID如下
1. .net core的runtime.json文件由微軟提供:查看runtime.json. 2. runtime.json的runeims節(jié)點(diǎn)下,定義了所有的RID字典表以及RID樹關(guān)系. 3. 根據(jù)*.deps.json依賴文件中的程序集定義RID標(biāo)識(shí),就可以判斷出依賴文件中指向的dll是否能在某一平臺(tái)運(yùn)行. 4. 當(dāng)程序發(fā)布為兼容模式時(shí),我們出可以使用runtime.json文件選擇性的加載平臺(tái)dll并運(yùn)行. 三.AssemblyLoadContext的加載原理public class PluginLoadContext : AssemblyLoadContext { private AssemblyDependencyResolver _resolver; public PluginLoadContext(string pluginFolder, params string[] commonAssemblyFolders) : base(isCollectible: true) { this.ResolvingUnmanagedDll += PluginLoadContext_ResolvingUnmanagedDll; this.Resolving += PluginLoadContext_Resolving; //第1步,解析des.json文件,并調(diào)用Load和LoadUnmanagedDll函數(shù) _resolver = new AssemblyDependencyResolver(pluginFolder); //第6步,通過第4,5步,解析仍失敗的dll會(huì)自動(dòng)嘗試調(diào)用主程序中的程序集, //如果失敗,則直接拋出程序集無法加載的錯(cuò)誤 } private Assembly PluginLoadContext_Resolving(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName) { //第4步,Load函數(shù)加載程序集失敗后,執(zhí)行的事件 } private IntPtr PluginLoadContext_ResolvingUnmanagedDll(Assembly assembly, string unmanagedDllName) { //第5步,LoadUnmanagedDll加載native dll失敗后執(zhí)行的事件 } protected override Assembly Load(AssemblyName assemblyName) { //第2步,先執(zhí)行程序集的加載函數(shù) } protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) { //第3步,先執(zhí)行的native dll加載邏輯 } }
微軟官方示例代碼如下:示例具體內(nèi)容class PluginLoadContext : AssemblyLoadContext { private AssemblyDependencyResolver _resolver; public PluginLoadContext(string pluginPath) { _resolver = new AssemblyDependencyResolver(pluginPath); } protected override Assembly Load(AssemblyName assemblyName) { string assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName); if (assemblyPath != null) { //加載程序集 return LoadFromAssemblyPath(assemblyPath); } //返回null,則直接加載主項(xiàng)目程序集 return null; } protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) { string libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName); if (libraryPath != null) { //加載native dll文件 return LoadUnmanagedDllFromPath(libraryPath); } //返回IntPtr.Zero,即null指針.將會(huì)加載主項(xiàng)中runtimes文件夾下的dll return IntPtr.Zero; } }
1. 官方這個(gè)示例是有問題的.LoadFromAssemblyPath()函數(shù)有bug, 2. Load和LoadUnmanagedDll函數(shù)實(shí)際上是給開發(fā)者手動(dòng)加載程序集使用的, 3. 手動(dòng)加載時(shí)可以根據(jù)deps.json文件定義的runtime加載當(dāng)前平臺(tái)下的unmanaged dll文件. 這些平臺(tái)相關(guān)的dll文件,一般位于發(fā)布目錄中的runtimes文件夾中. 四.插件項(xiàng)目一定要和主項(xiàng)目使用同樣的運(yùn)行時(shí).
五.AssemblyLoadContext.UnLoad()并不會(huì)拋出任何異常.當(dāng)你調(diào)用AssemblyLoadContext.UnLoad()卸載完插件以為相關(guān)程序集已經(jīng)釋放,那你可能就錯(cuò)了.
官方文檔表明卸載執(zhí)行失敗會(huì)拋出InvalidOperationException,不允許卸載官方說明。 六.反射程序集相關(guān)變量的定義為何阻止插件程序集卸載?插件namespace PluginSample { public class SimpleService { public void Run(string name) { Console.WriteLine($"Hello World!"); } } }
加載插件namespace Test { public class PluginLoader { pubilc AssemblyLoadContext assemblyLoadContext; public Assembly assembly; public Type type; public MethodInfo method; public void Load() { assemblyLoadContext = new PluginLoadContext("插件文件夾"); assembly = alc.Load(new AssemblyName("PluginSample")); type = assembly.GetType("PluginSample.SimpleService"); method=type.GetMethod() } } }
1. 在主項(xiàng)目程序中.AssemblyLoadContext,Assembly,Type,MethodInfo等不能直接定義在任何類中. 2. 參照官方文檔后了解了WeakReferece類.使用該類與AssemblyLoadContext關(guān)聯(lián),當(dāng)手動(dòng)GC清理時(shí), 3. 使用WeakReference關(guān)聯(lián)AssemblyLoadContext并判斷是否卸載成功 public void Load(out WeakReference weakReference) { var assemblyLoadContext = new PluginLoadContext("插件文件夾"); weakReference = new WeakReference(pluginLoadContext, true); assemblyLoadContext.UnLoad(); } public void Check() { WeakReference weakReference=null; Load(out weakReference); //一般第二次,IsAlive就會(huì)變?yōu)镕alse,即AssemblyLoadContext卸載失敗. for (int i = 0; weakReference.IsAlive && (i < 10); i++) { GC.Collect(); GC.WaitForPendingFinalizers(); } }
4. 為了解決以上問題.可以把需要的變量放到靜態(tài)字典中.在Unload之前把對(duì)應(yīng)的Key值刪除掉,即可. 七.程序集的異步函數(shù)執(zhí)行為何會(huì)阻止插件程序的卸載?public class SimpleService { //同步執(zhí)行,插件卸載成功 public void Run(string name) { Console.WriteLine($"Hello {name}!"); } //異步執(zhí)行,卸載成功 public Task RunAsync(string name) { Console.WriteLine($"Hello {name}!"); return Task.CompletedTask; } //異步執(zhí)行,卸載成功 public Task RunTask(string name) { return Task.Run(() => { Console.WriteLine($"Hello {name}!"); }); } //異步執(zhí)行,卸載成功 public Task RunWaitTask(string name) { return Task.Run( async ()=> { while (true) { if (CancellationTokenSource.IsCancellationRequested) { break; } await Task.Delay(1000); Console.WriteLine($"Hello {name}!"); } }); } //異步執(zhí)行,卸載成功 public Task RunWaitTaskForCancel(string name, CancellationToken cancellation) { return Task.Run(async () => { while (true) { if (cancellation.IsCancellationRequested) { break; } await Task.Delay(1000); Console.WriteLine($"Hello {name}!"); } }); } //異步執(zhí)行,卸載失敗 public async Task RunWait(string name) { while (true) { if (CancellationTokenSource.IsCancellationRequested) { break; } await Task.Delay(1000); Console.WriteLine($"Hello {name}!"); } } //異步執(zhí)行,卸載失敗 public Task RunWaitNewTask(string name) { return Task.Factory.StartNew(async ()=> { while (true) { if (CancellationTokenSource.IsCancellationRequested) { break; } await Task.Delay(1000); Console.WriteLine($"Hello {name}!"); } },TaskCreationOptions.DenyChildAttach); } }
1. 以上測(cè)試可以看出,如果插件調(diào)用的是一個(gè)常規(guī)帶wait的async異步函數(shù),則插件一定會(huì)卸載失敗. 2. 如果在插件中使用Task.Factory.StartNew函數(shù)也會(huì)調(diào)用失敗,原因不明. 八.正確卸載插件的方式
|
|