服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-10-24 38bde3e8210c41e9d0d219daba0c075cd676efce
优化dll加载
已修改4个文件
已添加1个文件
182 ■■■■■ 文件已修改
Tiger.Api/Autofac/AutoFacContianer.cs 25 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.Api/Autofac/PluginManager.cs 96 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.Api/Controllers/Extension.cs 13 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.Api/Controllers/Test/TestController.R.cs 46 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
Tiger.Business.MES/Transaction/TestNode.cs 2 ●●●●● 补丁 | 查看 | 原始文档 | 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;
        }