From 38bde3e8210c41e9d0d219daba0c075cd676efce Mon Sep 17 00:00:00 2001 From: Rodney Chen <rodney.chen@hotmail.com> Date: 星期四, 24 十月 2024 10:20:54 +0800 Subject: [PATCH] 优化dll加载 --- Tiger.Api/Autofac/AutoFacContianer.cs | 25 ++++- Tiger.Api/Controllers/Test/TestController.R.cs | 46 +++++++++++ Tiger.Api/Autofac/PluginManager.cs | 96 ++++++++++++++++++++++++ Tiger.Api/Controllers/Extension.cs | 13 +- Tiger.Business.MES/Transaction/TestNode.cs | 2 5 files changed, 169 insertions(+), 13 deletions(-) diff --git a/Tiger.Api/Autofac/AutoFacContianer.cs b/Tiger.Api/Autofac/AutoFacContianer.cs index db6b664..3ce35cd 100644 --- a/Tiger.Api/Autofac/AutoFacContianer.cs +++ b/Tiger.Api/Autofac/AutoFacContianer.cs @@ -22,6 +22,7 @@ /// 瀹瑰櫒 /// </summary> public static IContainer Instance; + public static AppDomain AppDomain; /// <summary> /// 鍒濆鍖栧鍣� @@ -46,14 +47,24 @@ #region 鍔ㄦ�佸姞杞藉涓猟ll string[] assemblyPattern = new[] { "Tiger.Business([.].*)*.dll" }; - // 1. Scan for assemblies containing autofac modules in the bin folder - List<Assembly> assemblies = new List<Assembly>(); - assemblies.AddRange( - Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll", SearchOption.AllDirectories) - .Where(filename => assemblyPattern.Any(pattern => Regex.IsMatch(filename, pattern))) - .Select(Assembly.LoadFrom) - ); + //AppDomain = AppDomain.CreateDomain("AutoFac Dll"); + //List<Assembly> assemblies = new List<Assembly>(); + //foreach (var file in Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll", SearchOption.AllDirectories).Where(filename => assemblyPattern.Any(pattern => Regex.IsMatch(filename, pattern)))) + //{ + // var assembly = AppDomain.Load(Assembly.LoadFrom(file).GetName()); + // assemblies.Add(assembly); + //} + // 1. Scan for assemblies containing autofac modules in the bin folder + //List<Assembly> assemblies = new List<Assembly>(); + //assemblies.AddRange( + // Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll", SearchOption.AllDirectories) + // .Where(filename => assemblyPattern.Any(pattern => Regex.IsMatch(filename, pattern))) + // .Select(Assembly.LoadFrom) + //); + + + var assemblies = PluginManager.Load(AppDomain.CurrentDomain.BaseDirectory, "Tiger.Business([.].*)*.dll"); foreach (var assembly in assemblies) { builder.RegisterAssemblyTypes(assembly).AsImplementedInterfaces(); diff --git a/Tiger.Api/Autofac/PluginManager.cs b/Tiger.Api/Autofac/PluginManager.cs new file mode 100644 index 0000000..202260a --- /dev/null +++ b/Tiger.Api/Autofac/PluginManager.cs @@ -0,0 +1,96 @@ +锘縰sing Rhea.Common; +using System.Collections.Generic; +using System.Reflection; +using Tiger.Model.Minsun; +using Tiger.Model; +using System.Linq; +using Autofac; +using System.IO; +using System.Text.RegularExpressions; +using System; +using System.Threading; +using System.Runtime.Loader; +using Tiger.IBusiness; + +namespace Tiger.Api +{ + public class PluginManager + { + public static List<Plugin> Plugins { get; set; } = new(); + + public static List<Assembly> Load(string path, params string[] patterns) + { + List<Assembly> assemblies = new(); + foreach (var file in Directory.EnumerateFiles(path, "*.dll", SearchOption.AllDirectories).Where(filename => patterns.Any(pattern => Regex.IsMatch(filename, pattern)))) + { + var plugin = new Plugin(file); + assemblies.Add(plugin.Assembly); + Plugins.Add(plugin); + } + return assemblies; + } + + }//endClass + + public class Plugin + { + public Plugin(string path) + { + Load(path); + } + + #region Variables + + #endregion + + #region Propertys + public string Id { get; set; } = Guid.NewGuid().ToString("N"); + public string Name { get; set; } + public string FullPath { get; set; } + public bool IsActive { get; set; } + public AssemblyLoadContext ALC { get; set; } + public Assembly Assembly { get; set; } + #endregion + + #region Functions + /// <summary> + /// 鍔犺浇 + /// </summary> + private void Load(string fullPath) + { + FullPath = fullPath; + //var file = new FileInfo(path); + Name = Path.GetFileNameWithoutExtension(FullPath); + + //ALC = new AssemblyLoadContext(Name, true); + //Assembly = ALC.LoadFromAssemblyPath(Path); + //ALC.Unloading += context => { + // IsActive = false; + // Logger.Default.Info($"鍗歌浇{context.Name}绋嬪簭闆嗭細" + string.Join(',', context.Assemblies.Select(x => x.FullName))); + //}; + ALC = AssemblyLoadContext.Default; + Assembly = ALC.LoadFromAssemblyPath(FullPath); + //Assembly.LoadFrom(path); + + IsActive = true; + } + + /// <summary> + /// 鍗歌浇 + /// </summary> + public void Unload() + { + ALC.Unload(); + } + + /// <summary> + /// 閲嶆柊鍔犺浇 + /// </summary> + public void Reload() + { + + } + + #endregion + }//endClass +} diff --git a/Tiger.Api/Controllers/Extension.cs b/Tiger.Api/Controllers/Extension.cs index dbc855b..5a8049d 100644 --- a/Tiger.Api/Controllers/Extension.cs +++ b/Tiger.Api/Controllers/Extension.cs @@ -15,13 +15,14 @@ // 鍔ㄦ�佸姞杞藉涓猟ll string[] assemblyPattern = new[] { "Tiger.Controllers([.].*)*.dll" }; - List<Assembly> assemblies = new List<Assembly>(); - assemblies.AddRange( - Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll", SearchOption.AllDirectories) - .Where(filename => assemblyPattern.Any(pattern => Regex.IsMatch(filename, pattern))) - .Select(Assembly.LoadFrom) - ); + //List<Assembly> assemblies = new List<Assembly>(); + //assemblies.AddRange( + // Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll", SearchOption.AllDirectories) + // .Where(filename => assemblyPattern.Any(pattern => Regex.IsMatch(filename, pattern))) + // .Select(Assembly.LoadFrom) + //); + var assemblies = PluginManager.Load(AppDomain.CurrentDomain.BaseDirectory, "Tiger.Controllers([.].*)*.dll"); foreach (var assembly in assemblies) { builder = builder.AddApplicationPart(assembly); diff --git a/Tiger.Api/Controllers/Test/TestController.R.cs b/Tiger.Api/Controllers/Test/TestController.R.cs index 0b48c83..61c346a 100644 --- a/Tiger.Api/Controllers/Test/TestController.R.cs +++ b/Tiger.Api/Controllers/Test/TestController.R.cs @@ -23,6 +23,8 @@ using Swifter.Tools; using Tiger.Model.Entitys.MES.Position; using Microsoft.CodeAnalysis.Options; +using System.IO; +using System.Runtime.Loader; namespace Tiger.Api.Controllers.Test { @@ -333,6 +335,50 @@ return Ok(result?.ToJson()); } + + [HttpGet] + public async Task<IActionResult> RefreashPlugin(string path) + { + var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == "Tiger.Business.MES"); + // 鏌ユ壘鎸囧畾鍚嶇О鐨勭▼搴忛泦 + //var assembly = Array.Find(assemblies, a => a.GetName().Name == "Tiger.Business.MES"); + + //var ass = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "\\Tiger.Business.MES1.dll"); + + + var mes1 = new AssemblyLoadContext("mes1", true); + var assembly1 = mes1.LoadFromAssemblyPath(AppDomain.CurrentDomain.BaseDirectory + "\\Tiger.Business1.MES.dll"); + mes1.Unloading += context => { Console.WriteLine($"褰撳墠鍗歌浇{context.Name}绋嬪簭闆嗭細" + string.Join(',', context.Assemblies.Select(x => x.FullName))); }; + assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == "Tiger.Business.MES"); + + var mes2 = new AssemblyLoadContext("mes2", true); + var assembly2 = mes2.LoadFromAssemblyPath(AppDomain.CurrentDomain.BaseDirectory + "\\Tiger.Business2.MES.dll"); + mes2.Unloading += context => { Console.WriteLine($"褰撳墠鍗歌浇{context.Name}绋嬪簭闆嗭細" + string.Join(',', context.Assemblies.Select(x => x.FullName))); }; + assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == "Tiger.Business.MES"); + + mes1.Unload(); + assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == "Tiger.Business.MES"); + mes2.Unload(); + assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.GetName().Name == "Tiger.Business.MES"); + + + + + + //var newBuilder = new ContainerBuilder(); + + ////AutoFac 閰嶇疆鏂囦欢娉ㄥ叆 + //AutoFacContainer.Init(newBuilder); + //newBuilder.RegisterBuildCallback(scope => + //{ + + // AutoFacContainer.Instance = (IContainer)scope; + //}); + //var newContainer = newBuilder.Build(); + //var trans = AutoFacContainer.Instance.Resolve<ITestNode>().Init("action.ID", "Request.Host.Value", "action.Data?.USER_CODE", "OQC001"); + //var d = trans.GetDefects(); + return Ok($""); + } } /// <summary> diff --git a/Tiger.Business.MES/Transaction/TestNode.cs b/Tiger.Business.MES/Transaction/TestNode.cs index ff0b8cf..dbe6279 100644 --- a/Tiger.Business.MES/Transaction/TestNode.cs +++ b/Tiger.Business.MES/Transaction/TestNode.cs @@ -655,6 +655,8 @@ return action; } action.Data = CurBatch.GetNodeDefects(PostCode); + //action.Data = new List<DefectOutput> { new DefectOutput { DFTG_CODE = "111111"} }; + //action.Data = new List<DefectOutput> { new DefectOutput { DFTG_CODE = "222222" } }; return action; } -- Gitblit v1.9.3