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

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:

  • SilkyFileProvider
  • IModuleLoader / ModuleLoader
  • INullCancellationTokenProvider

4. ModuleLoader (Dependency Graph Resolution)

ModuleLoader performs:

  1. Starts from the startup module T
  2. Reads [DependsOn(...)] attributes to discover all direct and indirect dependencies
  3. Performs topological sort to produce a deterministic load order
  4. 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's Initialize(applicationContext) in topological order — opens registry connections, starts DotNetty listeners, registers service routes, etc.
  • StopAsync(): Calls each module's Shutdown(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)
  • IServiceProvider wrapper — delegates to the Autofac root container after build
  • EngineContext.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;
Edit this page
Prev
Host Construction
Next
Module System