中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何開發NetCore插件

發布時間:2021-10-13 10:06:51 來源:億速云 閱讀:164 作者:iii 欄目:編程語言

這篇文章主要介紹“如何開發NetCore插件”,在日常操作中,相信很多人在如何開發NetCore插件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何開發NetCore插件”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

使用NetCore3.1完成框架基本開發后實際應用于項目,需要保證框架的獨立性與項目的個性化, 就需要類似于下圖的插件化形式將項目放入框架這個容器中啟動,下面開始詳細介紹實現步驟

如何開發NetCore插件

項目dll掃描

在框架根目錄創建Plugin文件夾,項目在生成后事件使用copy命令拷貝至Plugin文件夾,框架啟動時會掃描該文件夾并進行ShadowCopy到PluginTemplate文件夾中

private static void ScanPlugin()
        {if (!PluginFolder.Exists)
            {
                PluginFolder.Create();
            }if (!TempPluginFolder.Exists)
            {
                TempPluginFolder.Create();
            }

            TempPluginFolder.Attributes = FileAttributes.Normal & FileAttributes.Directory;
            PluginFolder.Attributes = FileAttributes.Normal & FileAttributes.Directory;//清理臨時文件。foreach (var file in TempPluginFolder.GetFiles("*.dll", SearchOption.AllDirectories))
            {try{
                    File.SetAttributes(file.FullName, FileAttributes.Normal);
                    file.Delete();
                }catch (Exception e)
                {throw new Exception("請檢查IIS權限");
                }

            }//復制插件進臨時文件夾。foreach (var plugin in PluginFolder.GetFiles("*.dll", SearchOption.AllDirectories))
            {try{string CopyFilePath = Path.Combine(TempPluginFolder.FullName, plugin.Name);
                    File.Copy(plugin.FullName, CopyFilePath, true);
                    File.SetAttributes(CopyFilePath, FileAttributes.Normal);
                }catch (Exception e)
                {throw new Exception("請檢查IIS權限");
                }
            }
        }

項目dll加載

直接使用ApplicationParts加載AssemblyPart即可

public static void LoadPlugin()
        {
            ScanPlugin();
            IEnumerable<Assembly> AssemblyList = GetPluginAssemblies();foreach (Assembly assembly in AssemblyList)
            {foreach (var type in assembly.GetTypes())
                {if (type.IsAssignableFrom(typeof(Controller)) && type.Name.Contains("Controller") && type.IsClass && !type.IsAbstract)
                    {string Name = type.Name;//如果有自定義的路由注解if (type.IsDefined(typeof(Route), false))
                        {var areaattribute = type.GetCustomAttributes(typeof(Route), false).FirstOrDefault();
                            Name = ((Route)areaattribute).Name;
                            Name += "Controller";

                        }if (!ControllerTypeDic.ContainsKey(Name))
                        {
                            ControllerTypeDic.Add(Name, type);
                        }
                    }
                }var controllerAssemblyPart = new AssemblyPart(assembly);
                CloudUtil.GetBuilder().ConfigureApplicationPartManager(apm =>{
                    apm.ApplicationParts.Add(controllerAssemblyPart);
                });
            }
            CloudUtil.GetBuilder().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
            StartPluginRefreshWatch();
        }

項目視圖

項目的cshtml視圖文件可以手動拷貝到部署的框架Views文件夾中(正式環境),也可以通過監控當前項目的Views文件夾,如果變動進行自動拷貝(開發環境)

private static void StartPluginRefreshWatch()
        {
            _FileSystemWatcher.Path = CloudUtil.GetContentPath() + "/Plugin";
            _FileSystemWatcher.Filter = "*.dll";
            _FileSystemWatcher.Changed += _FileSystemWatcher_Changed;

            _FileSystemWatcher.IncludeSubdirectories = true;
            _FileSystemWatcher.NotifyFilter =NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
            _FileSystemWatcher.EnableRaisingEvents = true;if (AppConfigUtil.Configuration["Frame:ViewsPublishType"]=="AutoCopy"&& !string.IsNullOrEmpty(AppConfigUtil.Configuration["Frame:ViewsVirtualPath"]) && Directory.Exists(AppConfigUtil.Configuration["Frame:ViewsVirtualPath"]))
            {
                FileSystemWatcher ViewsWatcher = new FileSystemWatcher();
                ViewsWatcher.Path = AppConfigUtil.Configuration["Frame:ViewsVirtualPath"];
                ViewsWatcher.Changed += ViewsWatcher_Changed;

                ViewsWatcher.IncludeSubdirectories = true;
                ViewsWatcher.NotifyFilter =NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
                ViewsWatcher.EnableRaisingEvents = true;//系統啟動時整個文件夾拷貝DirectoryInfo FrameWorkViews = new DirectoryInfo(CloudUtil.GetContentPath() + "/Views");
                DirectoryInfo ProjectViews = new DirectoryInfo(AppConfigUtil.Configuration["Frame:ViewsVirtualPath"]);foreach (DirectoryInfo FDir in FrameWorkViews.GetDirectories())
                {foreach (DirectoryInfo PDir in ProjectViews.GetDirectories())
                    {if (PDir.Name== FDir.Name)
                        {
                            FileUtil.DeleteDirectoryContent(FDir.FullName);
                        }
                    }
                }
                FileUtil.CopyDirectory(AppConfigUtil.Configuration["Frame:ViewsVirtualPath"],CloudUtil.GetContentPath() + "/Views", true);
            }
        }

項目dll熱更新

監控Plugin文件夾,若有變調用IApplicationLifetime的StopApplication重啟系統

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration configuration, Microsoft.AspNetCore.Hosting.IApplicationLifetime applicationLifetime)
 {
      CloudUtil.SetAPP(applicationLifetime);
 } private static void _FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
 {
      CloudUtil.Restrat();
 } public static void Restrat()
 {
      APP.StopApplication();
 }

到此,關于“如何開發NetCore插件”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。