Overview
IEngine is the core orchestrator of the Silky framework. EngineContext.Current provides a global singleton reference to the engine, which is available throughout the application lifetime.
Building the Service Engine
AddSilkyServices<T>() is the entry point (called from RegisterSilkyServices<T>()) for building the service engine:
public static IServiceCollection AddSilkyServices<T>(
this IServiceCollection services,
IConfiguration configuration)
where T : ISilkyModule
{
// Step 1: Create SilkyFileProvider for assembly scanning
ServiceLocator.SetLocator(services);
// Step 2: Create the SilkyEngine singleton and set in EngineContext
var engine = EngineContext.Create();
// Step 3: Register core infrastructure services
engine.SetHostedService(services);
// Step 4: Use ModuleLoader to resolve, sort, and load all modules
var moduleLoader = new ModuleLoader();
engine.LoadModules<T>(services, moduleLoader);
// Step 5: Register InitSilkyHostedService background task
services.AddHostedService<InitSilkyHostedService>();
// Step 6: Scan IConfigureService implementations and call ConfigureServices()
engine.ConfigureServices(services, configuration);
return services;
}
Key Responsibilities
1. SilkyFileProvider
The SilkyFileProvider is created early to enable:
- Assembly scanning for module discovery
- File path resolution for config and SSL cert files
2. EngineContext.Create()
Creates a SilkyEngine singleton and makes it accessible globally via EngineContext.Current. After this point, any code can call EngineContext.Current.Resolve<T>() to resolve services from the Autofac container.
3. Core Service Registration
Registers the minimum required services for the framework to function:
SilkyFileProviderIModuleLoader/ModuleLoaderINullCancellationTokenProvider
4. ModuleLoader (Dependency Graph Resolution)
ModuleLoader performs:
- Starts from the startup module
T - Reads
[DependsOn(...)]attributes to discover all direct and indirect dependencies - Performs topological sort to produce a deterministic load order
- Calls
SilkyModule.RegisterServices(builder)for each module (Autofac-level registration)
5. InitSilkyHostedService
A BackgroundService registered with .NET's hosted service infrastructure. At runtime:
StartAsync(): Calls each module'sInitialize(applicationContext)in topological order — opens registry connections, starts DotNetty listeners, registers service routes, etc.StopAsync(): Calls each module'sShutdown(applicationContext)in reverse order — gracefully closes connections, deregisters service routes, releases resources
6. engine.ConfigureServices()
Scans all assemblies for types implementing IConfigureService and calls their ConfigureServices(IServiceCollection, IConfiguration) methods. This is where each module registers its services into the Microsoft DI container.
SilkyEngine
SilkyEngine implements:
IEngine— lifecycle management (Initialize, Shutdown)IServiceProviderwrapper — delegates to the Autofac root container after buildEngineContext.Current— global access point
// Resolve services anywhere in the framework
var executor = EngineContext.Current.Resolve<IExecutor>();
// Check the current host name
var hostName = EngineContext.Current.HostName;
// Access the service provider directly
var sp = EngineContext.Current.ServiceProvider;
