ASP.NET Core MVC from scratch to achieve the development of plug-in (a) - Use ApplicationPart dynamic loading controllers and views

Original: scratch to achieve plug-in ASP.NET Core MVC development of (a) - Use ApplicationPart dynamic loading controllers and views

Title: plug-in from scratch to achieve the development of ASP.NET Core MVC of (a) - Use Application Part dynamic load controller and a view
of: Lamond Lu
Address: https: //www.cnblogs.com/lwqlun/p/11137788. html
source: https://github.com/lamondlu/Mystique

Preface #

If you've used some open source CMS, it will certainly be used in which the plug-in capabilities, users can dynamically add or upload some features by enabling plug-in package of the way, then how to achieve the development of plug-in ASP.NET Core MVC in it, Let's explore it.

This series just some of the author's attempt, does not mean necessarily correct, just to share some ideas, we can discuss what follow-up will be constantly updated.

What is ApplicationPart? #

Be developed in ASP.NET Core, then plug in, I have to say ApplicationPart. ApplicationPartASP.NET Core is an important component, it is an abstract application resources, which can be found by the controller contained in the assembly, view components, TagHelper and precompiled MVC Razor view and other features.

By default, when a ASP.NET Core MVC application starts, it will try to load the control in the current application project initiated projects and references, if you want never directly referenced assemblies and load controller precompiled Razor view, we need the help of ApplicationPartthe.

The ASP.NET Core MVC, there is a ApplicaitonPartManagerclass, by ApplicationPartManagerour current applications can be configured to use which ones ApplicationPart.

Example:

 
 
Copy
var assembly = Assembly.LoadFile("demo.dll"); var assemblyPart = new AssemblyPart(assembly); var mvcBuilders = services.AddMvc(); mvcBuilders.ConfigureApplicationPartManager(apm => { apm.ApplicationParts.Add(assemblyPart); });

Below it, we passed one of the most simple example, we show you how to help ApplicationPart, dynamic load third-party programs centralized controller and precompiled view.

Create a project #

First we create a ASP.NET Core MVC site, named DynamicPluginsDemoSite

Then we also create a .NET Core Class Library project named DemoPlugin1, while a reference to the project

  • Microsoft.AspNetCore.App
  • Microsoft.AspNetCore.Razor
  • Microsoft.AspNetCore.Razor.Design

Note: For more than three assemblies, it is necessary to ensure that the same version DynamicPluginsDemoSite and DemoPluigin1 use.

Here in order to ensure precompiled Razor view, we need to open the project file DemoPlugin1.csproj DemoPlugin1 project. The project uses the SDK from "Microsoft.NET.Sdk" changed to "Microsoft.Net.Sdk.Razor".

 
 
Copy
<Project Sdk="Microsoft.NET.Sdk.Razor"> <PropertyGroup> <TargetFramework>netcoreapp2.2</TargetFramework> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'"> <OutputPath>C:\Users\Lamond Lu\source\repos\DynamicPlugins\DynamicPluginsDemoSite\bin\Debug</OutputPath> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Razor" Version="2.2.0" /> <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" /> </ItemGroup> </Project>

Note: If you do not do this modification, after the last compile the project, will not produce an assembly view Razor precompiled. (Here if there are other more elegant way to modify, please leave a message, I will attempt to write the follow-up project template to avoid a repeat this operation).

Adding controllers and views #

Let's start writing our plugin.

Here we first create a Plugin1Controller.cs.

 
 
Copy
public class Plugin1Controller : Controller { public IActionResult HelloWorld() { return View(); } }

Then we add a corresponding view file HelloWorld.cshtml.

 
 
Copy
@{ } <h1>This is Demo Plugin1.</h1>

The final project file directory as follows:

Finally, we need to modify the output directory of a project, we need to send the item to the Debug directory compiled dll DynamicPluginsDemoSite project.

Above we completed all modifications of the first component, let's start modifying DynamicPluginsDemoSite project.

How to dynamically load the plug-in controller? #

Since DynamicPluginsDemoSite project not directly refer to DemoPlugin1, when the project started, it can not be independently found DemoPlugin1 project controllers, so here we need to use ApplicationPartto load DemoPlugin1 assembly to the current operating environment.

 
 
Copy
public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); var assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "DemoPlugin1.dll"); var mvcBuilders = services.AddMvc(); var controllerAssemblyPart = new AssemblyPart(assembly); mvcBuilders.ConfigureApplicationPartManager(apm => { apm.ApplicationParts.Add(controllerAssemblyPart); }); mvcBuilders.SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }

Code explanation:

  • Because in the previous step, we will DemoPlugin1 assembly output to the Debug directory DynamicPluginsDemoSite in, so we can use this Assembly.LoadFilemethod to load it.
  • Here we use AssemblyPartthe class, is loaded into a package assembly ApplicationPart.
  • mvcBuildersSubject ConfigureApplicationPartManagermethod can be used to configure the project currently used ApplicationPart

How to load pre-compiled components Razor view? #

After the controller has been loaded, we also need to load precompiled plugins Razor view. Here and before a little different, we need to use CompileRazorAssemblyPartprecompiled Razor view to encapsulate loaded.

 
 
Copy
public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); var assembly = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "DemoPlugin1.dll"); var assemblyView = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "DemoPlugin1.Views.dll"); var viewAssemblyPart = new CompiledRazorAssemblyPart(assemblyView); var controllerAssemblyPart = new AssemblyPart(assembly); var mvcBuilders = services.AddMvc(); mvcBuilders.ConfigureApplicationPartManager(apm => { apm.ApplicationParts.Add(controllerAssemblyPart); apm.ApplicationParts.Add(viewAssemblyPart); }); mvcBuilders.SetCompatibilityVersion(CompatibilityVersion.Version_2_2); }

The net effect #

Now we start DynamicPluginsDemoSite, input / Plugin1 / HelloWorld in the browser, our normal plugin enabled.

Note: Before starting DynamicPluginsDemoSite site, be sure to compile DemoPlugin1 projects, such DemoPlugin1 assemblies produced will be output to DynamicPluginsDemoSite in.

Summary #

These are just implemented a simple MVC plug-in function, in order to complete the entire project, the follow-up remains to be done

  • We need to create a plug-in templates, to avoid duplication of effort.
  • And business models need to plug the abstracts.
  • You need to use a database to store plug-in information.
  • The need to support and upgrade management plug-in plug-ins.

I will follow slowly achieve the above functions, we stay tuned.

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11904673.html