Silky Microservice FrameworkSilky Microservice Framework
Home
Docs
Config
Source
github
gitee
  • 简体中文
  • English
Home
Docs
Config
Source
github
gitee
  • 简体中文
  • English
  • Startup

    • Silky Framework Source Code Analysis
    • Host Construction
    • Service Engine
    • Module System
    • Service & Service Entry Resolution
    • Service Registration
    • Dependency Injection Conventions
    • RPC Service Proxy
  • Runtime

    • Endpoints & Routing
    • Executor Dispatch System
    • Local Executor & Server-Side Filters
    • Remote Executor & RPC Call Chain
    • RPC Server Message Handling
    • Service Governance
    • Cache Interceptor
    • Distributed Transactions (TCC)
    • HTTP Gateway Pipeline
    • Filter Pipeline
    • Polly Resilience Pipeline
    • Endpoint Health Monitor

Module Definition

Silky is a modular framework composed of multiple NuGet packages. Each module encapsulates a cohesive set of features with its own logic and scope, independent of other modules.

The Module Class

A module is defined by creating a class in the assembly that derives from SilkyModule:

public class RpcModule : SilkyModule
{
}

SilkyModule is an abstract class that also extends Autofac's Module:

public abstract class SilkyModule : Autofac.Module, ISilkyModule, IDisposable
{
    protected SilkyModule()
    {
        Name = GetType().Name.RemovePostFix(StringComparison.OrdinalIgnoreCase, "Module");
    }

    // Autofac-level service registration
    protected override void Load([NotNull] ContainerBuilder builder)
    {
        base.Load(builder);
        RegisterServices(builder);
    }

    // Microsoft DI-level service registration
    public virtual void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
    }

    // Additional Autofac registrations (override in derived modules)
    protected virtual void RegisterServices([NotNull] ContainerBuilder builder)
    {
    }

    // Called when the application starts
    public virtual Task Initialize([NotNull] ApplicationContext applicationContext)
    {
        return Task.CompletedTask;
    }

    // Called when the application stops
    public virtual Task Shutdown([NotNull] ApplicationContext applicationContext)
    {
        return Task.CompletedTask;
    }

    public virtual string Name { get; }
}

The Four Module Roles

MethodPhasePurpose
ConfigureServices()StartupRegister services into IServiceCollection (Microsoft DI)
RegisterServices()StartupRegister services into Autofac ContainerBuilder
Initialize()Application startLifecycle init: open connections, start DotNetty listeners, register service routes
Shutdown()Application stopCleanup: close connections, deregister routes, release resources

Module Discovery & Dependency Resolution

Modules declare their dependencies using the [DependsOn] attribute:

[DependsOn(typeof(SilkyRpcModule), typeof(SilkyCachingModule))]
public class OrderModule : SilkyModule
{
}

At startup, ModuleLoader:

  1. Starts from the user-specified startup module (e.g., AppModule)
  2. Recursively reads [DependsOn] attributes to discover all transitive dependencies
  3. Performs topological sort to produce a deterministic load order (dependencies load before dependents)
  4. Calls RegisterServices(builder) for each module in order to configure the Autofac container

Module Lifecycle (InitSilkyHostedService)

After the DI container is built, InitSilkyHostedService (an IHostedService) manages the runtime lifecycle:

public class InitSilkyHostedService : IHostedService
{
    private readonly IModuleManager _moduleManager;

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        // Call Initialize() on each module in topological order
        await _moduleManager.InitializeModules(applicationContext);
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        // Call Shutdown() on each module in reverse topological order
        await _moduleManager.ShutdownModules(applicationContext);
    }
}

HTTP Modules

For Web Host scenarios, modules that extend SilkyHttpModule also participate in ASP.NET Core middleware pipeline configuration:

public abstract class SilkyHttpModule : SilkyModule
{
    // Called during app.UseXxx() middleware setup
    public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    }
}

Configure() is called in topological order during the middleware pipeline build phase — after all services are registered and the container is built.

Edit this page
Prev
Service Engine
Next
Service & Service Entry Resolution