Implementation of a simple system of modules for dotnet applications.
Contains the base classes of the module and the launched module, methods of integration with Microsoft.Extensions.Hosting
and simply withIServiceCollection
, a background service for starting "Launched modules".
Integration takes place as follows:
- Creation of a starting module in the application (modules from other assemblies will be connected in it, services will be registered, background work will be started)
- Integration indicating the start module
- Getting dependent modules from the start module
- Registering dependencies from all modules
- Registering the background job launch service
- Registration of the list of modules in DI.
- The usual module
Module
. Allows you to register and configure all dependent services based on the dependency tree. - Module with background work
RunnubleModule
. Extension of a regular module with support for starting and stopping background work in modules. - Module with long running background work
BackgroundModule
. Extension of the background work module with support for starting and stopping long background work, which does not affect the start of the rest of the application and can work in the flesh until the application is closed. - Module with scheduled background work
ScheduledModule
. Extension of the background work module with support for starting background work with a certain frequency specified by Cron expression
Type [] DependsModules
- Field of the list of dependencies, which will be recursively collected into one list and all dependencies are configuredModulesConfiguration Configuration
- The field containing the configuration storage object. Allows you to get and change settings in the repository of a modular system to change the behavior of modulesConfigure (IServiceCollection services)
- Method in which dependencies are registered
StartAsync (IServiceProvider provider, CancellationToken cancellationToken)
- The method that starts the background work of the moduleStopAsync (CancellationToken cancellationToken)
- The method that stops the background work of the module
ExecuteAsync(IServiceProvider provider, CancellationToken cancellationToken = default)
- Method, launching long-running background work of the module without blocking further application launch
string CronExpression
- The field containing the Cron Expression on which the background work will be launchedExecuteAsync(IServiceProvider provider, CancellationToken cancellationToken = default)
- A method that runs in the background on a schedule
For use you needed install packages:
Install-Package Skidbladnir.Modules
Sample dependent module ( for sample registering LocalStorage):
public class DependentRunnubleModule: RunnableModule{
public override void Configure(IServiceCollection services)
{
var storageConfiguration = Configuration.Get<LocalFsStorageConfiguration>();
services.AddLocalFsStorage(storageConfiguration);
}
public override async Task StartAsync(IServiceProvider provider, CancellationToken cancellationToken)
{
var logger = provider.GetService<ILogger<StartupModule>>();
var localStorage = provider.GetService<IStorage<LocalStorageInfo>>();
var currentDir = await localStorage.GetFilesAsync("");
logger.LogInformation("Files in root dir");
foreach (var fileInfo in currentDir)
{
logger.LogInformation("Filename: {FileName}\t\t Length: {Length}\t\t Date: {Date}",
fileInfo.FileName,
fileInfo.Size, fileInfo.CreatedDate);
}
}
}
Sample Startup module:
public class StartupModule : Module
{
public override Type[] DependsModules => new[] { typeof(DependentRunnubleModule) };
}
For integration modular system with host, need use UseSkidbladnirModules<TModule>
extension on IHostBuilder
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseSkidbladnirModules<StartupModule>(configuration =>
{
var storageConfiguration =
configuration.AppConfiguration.GetSection("Storage").Get<LocalFsStorageConfiguration>();
configuration.Add(storageConfiguration);
});
}
In this case, under the hood there is a host that can launch IHostedService and you do not need to launch ModuleRunner.
public void ConfigureServices(IServiceCollection services)
{
services.UseSkidbladnirModules<StartupModule>();
}
In this case, there is no host to run the ModuleRunner, so it will need to be started manually
public static async Task Main(string[] args)
{
var services = new ServiceCollection();
services.AddLogging(b => b.AddConsole());
services.UseSkidbladnirModules<StartupModule>();
var provider = services.BuildServiceProvider();
await provider.StartModules();
}