從零開始實現ASP.NET Core MVC的外掛式開發(五) - 外掛的刪除和升級
標題:從零開始實現ASP.NET Core MVC的外掛式開發(五) - 使用AssemblyLoadContext實現外掛的升級和刪除
作者:Lamond Lu
地址:https://www.cnblogs.com/lwqlun/p/11395828.html
原始碼:https://github.com/lamondlu/DynamicPlugins
前景回顧:
- 從零開始實現ASP.NET Core MVC的外掛式開發(一) - 使用Application Part動態載入控制器和檢視
- 從零開始實現ASP.NET Core MVC的外掛式開發(二) - 如何建立專案模板
- 從零開始實現ASP.NET Core MVC的外掛式開發(三) - 如何在執行時啟用元件
- 從零開始實現ASP.NET Core MVC的外掛式開發(四) - 外掛安裝
簡介
在上一篇中,我為大家講解了如何實現外掛的安裝,在文章的最後,留下了兩個待解決的問題。
- .NET Core 2.2中不能實現執行時刪除外掛
- .NET Core 2.2中不能實現執行時升級外掛
其實這2個問題歸根結底其實都是一個問題,就是外掛程式集被佔用,不能在執行時更換程式集。在本篇中,我將分享一下我是如何一步一步解決這個問題的,其中也繞了不少彎路,查閱過資料,在.NET Core官方提過Bug,幾次差點想放棄了,不過最終是找到一個可行的方案。
.NET Core 2.2的遺留問題
程式集被佔用的原因
回顧一下,我們之前載入外掛程式集時所有使用的程式碼。
var provider = services.BuildServiceProvider(); using (var scope = provider.CreateScope()) { var unitOfWork = scope.ServiceProvider.GetService<IUnitOfWork>(); var allEnabledPlugins = unitOfWork.PluginRepository .GetAllEnabledPlugins(); foreach (var plugin in allEnabledPlugins) { var moduleName = plugin.Name; var assembly = Assembly.LoadFile($"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll"); var controllerAssemblyPart = new AssemblyPart(assembly); mvcBuilders.PartManager .ApplicationParts .Add(controllerAssemblyPart); } }
這裡我們使用了Assembly.LoadFile
方法載入了外掛程式集。 在.NET中使用Assembly.LoadFile
方法載入的程式集會被自動鎖定,不能執行任何轉移,刪除等造作,所以這就給我們刪除和升級外掛造成了很大困難。
PS: 升級外掛需要覆蓋已載入的外掛程式集,由於程式集鎖定,所以覆蓋操作不能成功。
使用AssemblyLoadContext
在.NET Framework中,如果遇到這個問題,常用的解決方案是使用AppDomain
類來實現外掛熱插拔,但是在.NET Core中沒有AppDomain
類。不過經過查閱,.NET Core 2.0之後引入了一個AssemblyLoadContext
類來替代.NET Freamwork中的AppDomain
。本以為使用它就能解決當前程式集佔用的問題,結果沒想到.NET Core 2.x版本提供的AssemblyLoadContext
沒有提供Unload
方法來釋放載入的程式集,只有在.NET Core 3.0版本中才為AssemblyLoadContext
類添加了Unload
方法。
相關連結:
- https://docs.microsoft.com/en-us/dotnet/api/system.runtime.loader.assemblyloadcontext?view=netcore-2.2
- https://docs.microsoft.com/en-us/dotnet/standard/assembly/unloadability-howto?view=netcore-2.2
升級.NET Core 3.0 Preview 8
因此,為了完成外掛的刪除和升級功能,我將整個專案升級到了最新的.NET Core 3.0 Preview 8版本。
這裡.NET Core 2.2升級到.NET Core 3.0有一點需要注意的問題。
在.NET Core 2.2中預設啟用了Razor檢視的執行時編譯,簡單點說就是.NET Core 2.2中自動啟用了讀取原始的Razor檢視檔案,並編譯檢視的功能。這就是我們在第三章和第四章中的實現方法,每個外掛檔案最終都放置在了一個Modules目錄中,每個外掛既有包含Controller/Action的程式集,又有對應的原始Razor檢視目錄Views,在.NET Core 2.2中當我們在執行時啟用一個元件之後,對應的Views可以自動載入。
The files tree is:
=================
|__ DynamicPlugins.Core.dll
|__ DynamicPlugins.Core.pdb
|__ DynamicPluginsDemoSite.deps.json
|__ DynamicPluginsDemoSite.dll
|__ DynamicPluginsDemoSite.pdb
|__ DynamicPluginsDemoSite.runtimeconfig.dev.json
|__ DynamicPluginsDemoSite.runtimeconfig.json
|__ DynamicPluginsDemoSite.Views.dll
|__ DynamicPluginsDemoSite.Views.pdb
|__ Modules
|__ DemoPlugin1
|__ DemoPlugin1.dll
|__ Views
|__ Plugin1
|__ HelloWorld.cshtml
|__ _ViewStart.cshtml
但是在.NET Core 3.0中,Razor檢視的執行時編譯需要引入程式集Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation
。並且在程式啟動時,需要啟動執行時編譯的功能。
public void ConfigureServices(IServiceCollection services)
{
...
var mvcBuilders = services.AddMvc()
.AddRazorRuntimeCompilation();
...
}
如果沒有啟用Razor檢視的執行時編譯,程式訪問外掛檢視的時候,就會報錯,提示檢視找不到。
使用.NET Core 3.0的AssemblyLoadContext載入程式集
這裡為了建立一個可回收的程式集載入上下文,我們首先基於AssemblyLoadcontext
建立一個CollectibleAssemblyLoadContext
類。其中我們將IsCollectible
屬性通過父類建構函式,將其設定為true。
public class CollectibleAssemblyLoadContext
: AssemblyLoadContext
{
public CollectibleAssemblyLoadContext()
: base(isCollectible: true)
{
}
protected override Assembly Load(AssemblyName name)
{
return null;
}
}
在整個外掛載入上下文的設計上,每個外掛都使用一個單獨的CollectibleAssemblyLoadContext
來載入,所有外掛的CollectibleAssemblyLoadContext
都放在一個PluginsLoadContext
物件中。
相關程式碼: PluginsLoadContexts.cs
public static class PluginsLoadContexts
{
private static Dictionary<string, CollectibleAssemblyLoadContext>
_pluginContexts = null;
static PluginsLoadContexts()
{
_pluginContexts = new Dictionary<string, CollectibleAssemblyLoadContext>();
}
public static bool Any(string pluginName)
{
return _pluginContexts.ContainsKey(pluginName);
}
public static void RemovePluginContext(string pluginName)
{
if (_pluginContexts.ContainsKey(pluginName))
{
_pluginContexts[pluginName].Unload();
_pluginContexts.Remove(pluginName);
}
}
public static CollectibleAssemblyLoadContext GetContext(string pluginName)
{
return _pluginContexts[pluginName];
}
public static void AddPluginContext(string pluginName,
CollectibleAssemblyLoadContext context)
{
_pluginContexts.Add(pluginName, context);
}
}
程式碼解釋:
- 當載入外掛的時候,我們需要將當前外掛的程式集載入上下文放到
_pluginContexts
字典中。字典的key是外掛的名稱,字典的value是外掛的程式集載入上下文。 - 當移除一個外掛的時候,我們需要使用
Unload
方法,來釋放當前的程式集載入上下文。
在完成以上程式碼之後,我們更改程式啟動和啟用元件的程式碼,因為這兩部分都需要將外掛程式集載入到CollectibleAssemblyLoadContext
中。
Startup.cs
var provider = services.BuildServiceProvider();
using (var scope = provider.CreateScope())
{
var option = scope.ServiceProvider
.GetService<MvcRazorRuntimeCompilationOptions>();
var unitOfWork = scope.ServiceProvider
.GetService<IUnitOfWork>();
var allEnabledPlugins = unitOfWork.PluginRepository
.GetAllEnabledPlugins();
foreach (var plugin in allEnabledPlugins)
{
var context = new CollectibleAssemblyLoadContext();
var moduleName = plugin.Name;
var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll";
var assembly = context.LoadFromAssemblyPath(filePath);
var controllerAssemblyPart = new AssemblyPart(assembly);
mvcBuilders.PartManager.ApplicationParts
.Add(controllerAssemblyPart);
PluginsLoadContexts.AddPluginContext(plugin.Name, context);
}
}
PluginsController.cs
public IActionResult Enable(Guid id)
{
var module = _pluginManager.GetPlugin(id);
if (!PluginsLoadContexts.Any(module.Name))
{
var context = new CollectibleAssemblyLoadContext();
_pluginManager.EnablePlugin(id);
var moduleName = module.Name;
var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll";
context.
var assembly = context.LoadFromAssemblyPath(filePath);
var controllerAssemblyPart = new AssemblyPart(assembly);
_partManager.ApplicationParts.Add(controllerAssemblyPart);
MyActionDescriptorChangeProvider.Instance.HasChanged = true;
MyActionDescriptorChangeProvider.Instance.TokenSource.Cancel();
PluginsLoadContexts.AddPluginContext(module.Name, context);
}
else
{
var context = PluginsLoadContexts.GetContext(module.Name);
var controllerAssemblyPart = new AssemblyPart(context.Assemblies.First());
_partManager.ApplicationParts.Add(controllerAssemblyPart);
_pluginManager.EnablePlugin(id);
MyActionDescriptorChangeProvider.Instance.HasChanged = true;
MyActionDescriptorChangeProvider.Instance.TokenSource.Cancel();
}
return RedirectToAction("Index");
}
意外結果
完成以上程式碼之後,我立刻嘗試了刪除程式集的操作,但是得到的結果卻不是我想要的。
雖然.NET Core 3.0為AssemblyLoadContext
提供了Unload
方法,但是呼叫之後, 你依然會得到一個檔案被佔用的錯誤
暫時不知道這是不是.NET Core 3.0的bug, 還是功能就是這麼設計的,反正感覺這條路是走不通了,折騰了一天,在網上找了好多方案,但是都不能解決這個問題。
就在快放棄的時候,突然發現AssemblyLoadContext
類提供了另外一種載入程式集的方式LoadFromStream
。
改用LoadFromStream載入程式集
看到LoadFromStream
方法之後,我的第一思路就是可以使用FileStream
載入外掛程式集,然後將獲得的檔案流傳給LoadFromStream
方法,並在檔案載入完畢之後,釋放掉這個FileStream
物件。
根據以上思路,我將載入程式集的方法修改如下
PS: Enable方法的修改方式類似,這裡我就不重複寫了。
var provider = services.BuildServiceProvider();
using (var scope = provider.CreateScope())
{
var option = scope.ServiceProvider
.GetService<MvcRazorRuntimeCompilationOptions>();
var unitOfWork = scope.ServiceProvider.GetService<IUnitOfWork>();
var allEnabledPlugins = unitOfWork.PluginRepository.GetAllEnabledPlugins();
foreach (var plugin in allEnabledPlugins)
{
var context = new CollectibleAssemblyLoadContext();
var moduleName = plugin.Name;
var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll";
_presetReferencePaths.Add(filePath);
using (var fs = new FileStream(filePath, FileMode.Open))
{
var assembly = context.LoadFromStream(fs);
var controllerAssemblyPart = new AssemblyPart(assembly);
mvcBuilders.PartManager.ApplicationParts.Add(controllerAssemblyPart);
PluginsLoadContexts.AddPluginContext(plugin.Name, context);
}
}
}
修改之後,我又試了一下刪除外掛的程式碼,果然成功刪除了。
"Empty path name is not legal. "問題
就在我認為功能已經全部完成之後,我又重新安裝了刪除的外掛,嘗試訪問外掛中的controller/action, 結果得到了意想不到的錯誤,外掛的中包含的頁面打不開了。
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[1]
An unhandled exception has occurred while executing the request.
System.ArgumentException: Empty path name is not legal. (Parameter 'path')
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.CreateMetadataReference(String path)
at System.Linq.Enumerable.SelectListIterator`2.ToList()
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.GetCompilationReferences()
at System.Threading.LazyInitializer.EnsureInitializedCore[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
at System.Threading.LazyInitializer.EnsureInitialized[T](T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory)
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RazorReferenceManager.get_CompilationReferences()
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.LazyMetadataReferenceFeature.get_References()
at Microsoft.CodeAnalysis.Razor.CompilationTagHelperFeature.GetDescriptors()
at Microsoft.AspNetCore.Razor.Language.DefaultRazorTagHelperBinderPhase.ExecuteCore(RazorCodeDocument codeDocument)
at Microsoft.AspNetCore.Razor.Language.RazorEnginePhaseBase.Execute(RazorCodeDocument codeDocument)
at Microsoft.AspNetCore.Razor.Language.DefaultRazorEngine.Process(RazorCodeDocument document)
at Microsoft.AspNetCore.Razor.Language.DefaultRazorProjectEngine.ProcessCore(RazorCodeDocument codeDocument)
at Microsoft.AspNetCore.Razor.Language.RazorProjectEngine.Process(RazorProjectItem projectItem)
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.CompileAndEmit(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation.RuntimeViewCompiler.OnCacheMiss(String normalizedPath)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Razor.Compilation.DefaultRazorPageFactoryProvider.CreateFactory(String relativePath)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.CreateCacheResult(HashSet`1 expirationTokens, String relativePath, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.OnCacheMiss(ViewLocationExpanderContext expanderContext, ViewLocationCacheKey cacheKey)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.LocatePageFromViewLocations(ActionContext actionContext, String pageName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.Razor.RazorViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine.FindView(ActionContext context, String viewName, Boolean isMainPage)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.FindView(ActionContext actionContext, ViewResult viewResult)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.SetRoutingAndContinue(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
這個檔案路徑非法的錯誤讓我感覺很奇怪,為什麼會有這種問題呢?與之前的程式碼的不同之處只有一個地方,就是從LoadFromAssemblyPath
改為了LoadFromStream
。
為了弄清這個問題,我clone了最新的.NET Core 3.0 Preview 8原始碼,發現了在 .NET Core執行時編譯檢視的時候,會呼叫如下方法。
RazorReferenceManager.cs
internal IEnumerable<string> GetReferencePaths()
{
var referencePaths = new List<string>();
foreach (var part in _partManager.ApplicationParts)
{
if (part is ICompilationReferencesProvider compilationReferenceProvider)
{
referencePaths.AddRange(compilationReferenceProvider.GetReferencePaths());
}
else if (part is AssemblyPart assemblyPart)
{
referencePaths.AddRange(assemblyPart.GetReferencePaths());
}
}
referencePaths.AddRange(_options.AdditionalReferencePaths);
return referencePaths;
}
這段程式碼意思是根據當前載入程式集的所在位置,來發現對應檢視。
那麼問題就顯而易見了,我們之前用LoadFromAssemblyPath
載入程式集,程式集的檔案位置被自動記錄下來,但是我們改用LoadFromStream
之後,所需的檔案位置資訊丟失了,是一個空字串,所以.NET Core在嘗試載入檢視的時候,遇到空字串,丟擲了一個非法路徑的錯誤。
其實這裡的方法很好改,只需要將空字串的路徑排除掉即可。
internal IEnumerable<string> GetReferencePaths()
{
var referencePaths = new List<string>();
foreach (var part in _partManager.ApplicationParts)
{
if (part is ICompilationReferencesProvider compilationReferenceProvider)
{
referencePaths.AddRange(compilationReferenceProvider.GetReferencePaths());
}
else if (part is AssemblyPart assemblyPart)
{
referencePaths.AddRange(assemblyPart.GetReferencePaths().Where(o => !string.IsNullOrEmpty(o));
}
}
referencePaths.AddRange(_options.AdditionalReferencePaths);
return referencePaths;
}
但是由於不清楚會不會導致其他問題,所以我沒有采取這種方法,我將這個問題作為一個Bug提交到了官方。
問題地址: https://github.com/aspnet/AspNetCore/issues/13312
沒想到僅僅8小時,就得到官方的解決方案。
這段意思是說ASP.NET Core暫時不支援動態載入程式集,如果要在當前版本實現功能,需要自己實現一個AssemblyPart
類, 在獲取程式集路徑的時候,返回空集合而不是空字串。
PS: 官方已經將這個問題放到了.NET 5 Preview 1中,相信.NET 5中會得到真正的解決。
根據官方的方案,Startup.cs檔案的最終版本
public class MyAssemblyPart : AssemblyPart, ICompilationReferencesProvider
{
public MyAssemblyPart(Assembly assembly) : base(assembly) { }
public IEnumerable<string> GetReferencePaths() => Array.Empty<string>();
}
public static class AdditionalReferencePathHolder
{
public static IList<string> AdditionalReferencePaths = new List<string>();
}
public class Startup
{
public IList<string> _presets = new List<string>();
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.Configure<ConnectionStringSetting>(Configuration.GetSection("ConnectionStringSetting"));
services.AddScoped<IPluginManager, PluginManager>();
services.AddScoped<IUnitOfWork, UnitOfWork>();
var mvcBuilders = services.AddMvc()
.AddRazorRuntimeCompilation(o =>
{
foreach (var item in _presets)
{
o.AdditionalReferencePaths.Add(item);
}
AdditionalReferencePathHolder.AdditionalReferencePaths = o.AdditionalReferencePaths;
});
services.Configure<RazorViewEngineOptions>(o =>
{
o.AreaViewLocationFormats.Add("/Modules/{2}/Views/{1}/{0}" + RazorViewEngine.ViewExtension);
o.AreaViewLocationFormats.Add("/Views/Shared/{0}.cshtml");
});
services.AddSingleton<IActionDescriptorChangeProvider>(MyActionDescriptorChangeProvider.Instance);
services.AddSingleton(MyActionDescriptorChangeProvider.Instance);
var provider = services.BuildServiceProvider();
using (var scope = provider.CreateScope())
{
var option = scope.ServiceProvider.GetService<MvcRazorRuntimeCompilationOptions>();
var unitOfWork = scope.ServiceProvider.GetService<IUnitOfWork>();
var allEnabledPlugins = unitOfWork.PluginRepository.GetAllEnabledPlugins();
foreach (var plugin in allEnabledPlugins)
{
var context = new CollectibleAssemblyLoadContext();
var moduleName = plugin.Name;
var filePath = $"{AppDomain.CurrentDomain.BaseDirectory}Modules\\{moduleName}\\{moduleName}.dll";
_presets.Add(filePath);
using (var fs = new FileStream(filePath, FileMode.Open))
{
var assembly = context.LoadFromStream(fs);
var controllerAssemblyPart = new MyAssemblyPart(assembly);
mvcBuilders.PartManager.ApplicationParts.Add(controllerAssemblyPart);
PluginsLoadContexts.AddPluginContext(plugin.Name, context);
}
}
}
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(routes =>
{
routes.MapControllerRoute(
name: "Customer",
pattern: "{controller=Home}/{action=Index}/{id?}");
routes.MapControllerRoute(
name: "Customer",
pattern: "Modules/{area}/{controller=Home}/{action=Index}/{id?}");
});
}
}
外掛刪除和升級的程式碼
解決了程式集佔用問題之後,我們就可以開始編寫刪除/升級外掛的程式碼了。
刪除外掛
如果要刪除一個外掛,我們需要完成以下幾個步驟
- 刪除元件記錄
- 刪除元件遷移的表結構
- 移除載入過的ApplicationPart
- 重新整理Controller/Action
- 移除元件對應的程式集載入上下文
- 刪除元件檔案
根據這個步驟,我編寫了一個Delete
方法,程式碼如下:
public IActionResult Delete(Guid id)
{
var module = _pluginManager.GetPlugin(id);
_pluginManager.DisablePlugin(id);
_pluginManager.DeletePlugin(id);
var moduleName = module.Name;
var matchedItem = _partManager.ApplicationParts.FirstOrDefault(p =>
p.Name == moduleName);
if (matchedItem != null)
{
_partManager.ApplicationParts.Remove(matchedItem);
matchedItem = null;
}
MyActionDescriptorChangeProvider.Instance.HasChanged = true;
MyActionDescriptorChangeProvider.Instance.TokenSource.Cancel();
PluginsLoadContexts.RemovePluginContext(module.Name);
var directory = new DirectoryInfo($"{AppDomain.CurrentDomain.BaseDirectory}Modules/{module.Name}");
directory.Delete(true);
return RedirectToAction("Index");
}
升級外掛
對於升級外掛的程式碼,我將它和新增外掛的程式碼放在了一起
public void AddPlugins(PluginPackage pluginPackage)
{
var existedPlugin = _unitOfWork.PluginRepository.GetPlugin(pluginPackage.Configuration.Name);
if (existedPlugin == null)
{
InitializePlugin(pluginPackage);
}
else if (new DomainModel.Version(pluginPackage.Configuration.Version) > new DomainModel.Version(existedPlugin.Version))
{
UpgradePlugin(pluginPackage, existedPlugin);
}
else
{
DegradePlugin(pluginPackage);
}
}
private void InitializePlugin(PluginPackage pluginPackage)
{
var plugin = new DTOs.AddPluginDTO
{
Name = pluginPackage.Configuration.Name,
DisplayName = pluginPackage.Configuration.DisplayName,
PluginId = Guid.NewGuid(),
UniqueKey = pluginPackage.Configuration.UniqueKey,
Version = pluginPackage.Configuration.Version
};
_unitOfWork.PluginRepository.AddPlugin(plugin);
_unitOfWork.Commit();
var versions = pluginPackage.GetAllMigrations(_connectionString);
foreach (var version in versions)
{
version.MigrationUp(plugin.PluginId);
}
pluginPackage.SetupFolder();
}
public void UpgradePlugin(PluginPackage pluginPackage, PluginViewModel oldPlugin)
{
_unitOfWork.PluginRepository.UpdatePluginVersion(oldPlugin.PluginId,
pluginPackage.Configuration.Version);
_unitOfWork.Commit();
var migrations = pluginPackage.GetAllMigrations(_connectionString);
var pendingMigrations = migrations.Where(p => p.Version > oldPlugin.Version);
foreach (var migration in pendingMigrations)
{
migration.MigrationUp(oldPlugin.PluginId);
}
pluginPackage.SetupFolder();
}
public void DegradePlugin(PluginPackage pluginPackage)
{
throw new NotImplementedException();
}
程式碼解釋:
- 這裡我首先判斷了當前外掛包和已安裝版本的版本差異
- 如果系統沒有安裝過當前外掛,就安裝外掛
- 如果當前外掛包的版本比已安裝的版本高,就升級外掛
- 如果當前外掛包的版本比已安裝的版本低,就降級外掛(現實中這種情況不多)
InitializePlugin
是用來載入新元件的,它的內容就是之前的新增外掛方法UpgradePlugin
是用來升級元件的,當我們升級一個元件的時候,我們需要做一下幾個事情- 升級元件版本
- 做最新版本元件的指令碼遷移
- 使用最新程式包覆蓋老程式包
DegradePlugin
是用來降級元件的,由於篇幅問題,我就不詳細寫了,大家可以自行填補。
最終效果
總結
本篇中,我為大家演示如果使用.NET Core 3.0的AssemblyLoadContext
來解決已載入程式集佔用的問題,以此實現了外掛的升級和降級。本篇的研究時間較長,因為中間出現的問題確實太多了,沒有什麼可以複用的方案,我也不知道是不是第一個在.NET Core中這麼嘗試的。不過結果還算好,想實現的功能最終還是做出來了。後續呢,這個專案會繼續新增新的功能,希望大家多多支援。
專案地址:https://github.com/lamondlu/DynamicPlugins