This library allows to import your class libraries with controllers to your main project. So you can easily use plug-in & plug-out your features.
Firstly you'll need a main project and at least one module project. Let's start with creating them
-
Create an AspNetCore project as main web project.
-
Add Modularity.AspNetCore package to your project.
-
Go your Startup and add following codes to MvcBuilder in ConfigureServices():
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddModularity(); // <-- Add this line after AddMvc method
services.AddModuleServices(Configuration); // <-- Add this to add module's services into DI Container.
}
- Go Configure() method and add following code:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseModulartiy(env); // <-- Call UseModularity with HostingEnvirorment parameter
//...
}
-
That's it! Your Main Application is ready to load all modules from Plugins folder.
-
(OPTIONAL) You can manage your modules with config file. Just create following plugins.json file under Plugins folder:
[
{
"Name": "MyFirstPlugin",
"IsActive": true,
"LoadAllDependencies" : true
},
{
"Name": "MySecondPlugin",
"IsActive": false,
"LoadAllDependencies" : true
}
]
If you use configuration file, you need to place your modules their own folders like this:
To automaticly copy after build your modules with folders, change build action like this:
xcopy "$(OutDir)*" "$(SolutionDir)MyMainWebApplication\Plugins\$(ProjectName)\" /Y
MyMainWebApplication: This is your main host application.
-
Create a .Net Core class library.
-
Add Modularity.AspNetCore.Abstractions package to project.
-
Add Modularity.Core.Abstractions package to project.
-
Create a class and inherit from
IModuleStartup
. This class will be used initialization.
public class Startup : IModuleStartup
{
public IConfiguration Configuration { get; private set; }
public void Initialize(IConfiguration configuration)
{
this.Configuration = configuration; // Get main application's configuration and keep it to use in ConfigureServices()
}
public void ConfigureServices(IServiceCollection services)
{
/* Do your services configurations */
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
/* Do your application configurations */
}
}
- (OPTIONAL) You can add following code to Build Events to copy your module DLLs to your main application's
Plugins
folder. If they're in same solution.
xcopy "$(OutDir)*" "$(SolutionDir)MyMainWebApplication\Plugins\" /Y
MyMainWebApplication: Your main web application name.