In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to develop NetCore plug-ins". In daily operation, I believe many people have doubts about how to develop NetCore plug-ins. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to develop NetCore plug-ins"! Next, please follow the editor to study!
After the basic development of the framework is completed with NetCore3.1, it is actually applied to the project, which needs to ensure the independence of the framework and the personalization of the project. The plug-in form similar to the following figure is needed to put the project into the framework container to start. Here are the implementation steps in detail.
Project dll scan
Create a Plugin folder in the root directory of the frame, and the project is copied to the Plugin folder using the copy command after the build event. When the framework starts, it scans the folder and ShadowCopy it to the PluginTemplate folder.
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;// cleans up temporary files. Foreach (var file in TempPluginFolder.GetFiles ("* .dll", SearchOption.AllDirectories)) {try {File.SetAttributes (file.FullName, FileAttributes.Normal); file.Delete ();} catch (Exception e) {throw new Exception ("Please check IIS permissions") }} / / copy the plug-in into the temporary folder. 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 ("Please check IIS permissions") } Project dll load
You can load AssemblyPart directly using ApplicationParts.
Public static void LoadPlugin () {ScanPlugin (); IEnumerable 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 there is a custom route annotation 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 ();} Project View
The cshtml view file of the project can be manually copied to the deployed framework Views folder (formal environment), or it can be automatically copied (development environment) by monitoring the Views folder of the current project.
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 = copy of the entire folder when the true;// system starts up 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);} Project dll hot update
Monitor the Plugin folder. If there is any change, call IApplicationLifetime's StopApplication to restart the system
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 ();} at this point, the study on "how to develop NetCore plug-ins" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.