Repository navigation
02. Bootstraping
ogoun edited this page Apr 23, 2026
·
2 revisions
Bootstrap is the canonical entry point for any long-running ZeroLevel-based service. It wires up logging, configuration and the network stack, and gives you a fluent API to shape the host.
using ZeroLevel;
class Program
{
static void Main(string[] args)
{
Configuration.Save(Configuration.ReadFromApplicationConfig());
Bootstrap.Startup<MyService>(args,
() => Configuration.ReadSetFromIniFile("config.ini"))
.EnableConsoleLog(
ZeroLevel.Services.Logging.LogLevel.System |
ZeroLevel.Services.Logging.LogLevel.FullDebug)
.UseDiscovery() // optional — enables service discovery
.Run() // start the service
.WaitWhileStatus(ZeroServiceStatus.Running); // block main thread until Stop
Bootstrap.Shutdown(); // flush logs, close sockets, dispose
}
}Every service inherits from BaseZeroService and overrides StartAction / StopAction:
public sealed class MyService : BaseZeroService
{
protected override void StartAction()
{
Log.Info("MyService is starting up");
// init: network host, schedules, background loops
}
protected override void StopAction()
{
Log.Info("MyService is stopping");
// cleanup: cancel loops, close files
}
}Inside StartAction, expose handlers through UseHost():
protected override void StartAction()
{
var router = UseHost(8800); // bind TCP on port 8800
router.RegisterMessageHandler<Ping>((client, msg) =>
Log.Info($"ping from {client.EndPoint}"));
router.RegisterRequester<PingRequest, PongResponse>((client, req) =>
new PongResponse { Ts = DateTime.UtcNow });
}See 07. Network for the full TCP surface.
ZeroLevel composes cleanly with Topshelf to host the same service as a Windows Service:
IServiceExecution se = null;
Configuration.Save(Configuration.ReadFromApplicationConfig());
HostFactory.Run(x =>
{
x.StartAutomatically();
x.Service<BootstrapFluent>(s =>
{
s.ConstructUsing(_ => Bootstrap.Startup<MyService>(args)
.EnableConsoleLog(LogLevel.FullDebug));
s.WhenStopped(fluent => { fluent.Stop(); Bootstrap.Shutdown(); });
s.WhenStarted(fluent => { se = fluent.Run(); });
});
x.RunAsLocalSystem();
x.SetDescription("MyService");
x.SetDisplayName("MyService");
x.SetServiceName("MyService");
x.OnException(ex => Log.Error(ex, "Service exception"));
});
if (Environment.UserInteractive)
{
se?.WaitWhileStatus(ZeroServiceStatus.Running);
}Bootstrap.Startup<T>(...) returns a BootstrapFluent:
| Method | Effect |
|---|---|
.EnableConsoleLog(LogLevel) |
Attach a console logger. |
.EnableFileLog(path, options) |
Attach a rolling text-file logger. |
.EnableEncryptedFileLog(options) |
Attach an encrypted file logger. |
.UseDiscovery() / .UseDiscovery(url) / .UseDiscovery(IPEndPoint)
|
Register the service with a discovery hub. |
.Run() |
Start the service; returns IServiceExecution. |
.Stop() |
Stop the service (when invoked on the BootstrapFluent). |
- Load configuration (from app.config / ini / args).
-
Configuration.Save(...)— make it the process default. -
Bootstrap.Startup<TService>(args, () => ...)— construct the host. - Chain
.EnableXxxLog(...)/.UseDiscovery()— attach cross-cutting concerns. -
.Run().WaitWhileStatus(ZeroServiceStatus.Running)— block main thread. -
Bootstrap.Shutdown()— tear everything down (logs, network, schedulers).
Application basics
Data & serialization
Networking
Text & search