Recently I have been working on a large ASP.Net Core web application that also needs to have pluggable components so the main application can be deployed with or without them. It is possible to add a reference to another project in the Visual Studio Solution and have what are called Areas, but I don’t want this dependency so I went for Application Parts.
An application part is a .net assembly that can be used in the main application through reflection, you just need to tell the Startup class to look for them and include them with any other services.
This can be done through the ConfigureServices method in startup.cs
The ConfigureApplicationParts method is where we search for a Plugins directory and iterate through any libraries we find.
This Plugins directory must be in the bin directory of the main application like this:-
When this is done, the Plugin project creates a plugin.dll and a plugin.View.dll which is then loaded within the ConfigureApplicationParts method and depending on its name will be added to the ApplicationParts collection. As can be seen in this code above, it is expecting compiled Razor views. Lets go over this and see how we can embed these resources.
In the Plugin project, right click the project in Solution Explorer and choose Edit Project File
Make sure there is an element for ItemGroup and add the views as an embedded resource.
The **\* tells it to include all subdirectories.
This can also be done when editing the project file by removing and adding the resources like this:-
But occasionally I have had issues with this method and the project has refused to load after saving.
Out of the box, a ASP.Net Core 3.0 MVC application doesn’t have a styles section in the Layout file, so add one after the other CSS file references in the main application like this:-
Where the PageViewModel is:-
Again, feel free to refactor, but this will get you up and running.
The name of the resource by default will be the namespace of the application appended with the directory structure of the resource, but if you are unsure of what to insert into GetManifestResourceStream(), you can set the Plugin project as the startup project, debug and look at:-
So now the PageViewModel contains both resources as strings, so in the View add the model and the sections for scripts and styles.
Again for simplicity sake I have this as the plugin.css style:-
This as the plugin.js script:-
Build the Plugin project and drop the Plugin.dll and Plugin.Views.dll into the main application plugin directory mentioned earlier. If you already have the main application running in IIS Express, you will need to stop it by right clicking the icon and choosing Exit.
Finally set the main application as the startup project and run it. As I have my plugin controller route set to /Plugin/Index, you can navigate to this path and the plugin will display its very simple message. Also with the browser developer tools open you can see the console.log message as well.
You can find this solution on GitHub here:- https://github.com/asteropesystems/Application-Parts-Sample-Solution