Tiger.Api/Autofac/AutoFacContianer.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Tiger.Api/Autofac/PluginManager.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Tiger.Api/Controllers/Extension.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Tiger.Api/Controllers/Test/TestController.R.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
Tiger.Business.MES/Transaction/TestNode.cs | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
Tiger.Api/Autofac/AutoFacContianer.cs
@@ -22,6 +22,7 @@ /// å®¹å¨ /// </summary> public static IContainer Instance; public static AppDomain AppDomain; /// <summary> /// åå§åå®¹å¨ @@ -46,14 +47,24 @@ #region 卿å è½½å¤ä¸ªdll 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(); Tiger.Api/Autofac/PluginManager.cs
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,96 @@ using 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 } Tiger.Api/Controllers/Extension.cs
@@ -15,13 +15,14 @@ // 卿å è½½å¤ä¸ªdll 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); 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> 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; }