Hosting
Hosting
๐ Language: English | Espaรฑol
Generic host for Delphi applications inspired by .NETโs Microsoft.Extensions.Hosting. Composes services, configuration, hosted services (background workers) and the application lifetime into a single, managed runtime.
Why use this?
- ๐๏ธ Unified startup โ One place for DI, configuration, logging and background services
- ๐ Environment-aware โ Built-in
Development/Staging/Production/Testingmodes - โ๏ธ Layered configuration โ Host config, then app config, with environment variable overrides
- ๐ Hosted services โ Register
IHostedServiceimplementations that start and stop with the host - ๐ Graceful shutdown โ
IHostApplicationLifetimesignalsStarted,Stopping,Stopped - ๐ DI integration โ
IServiceProvideris the backbone; all services registered viaIServiceCollection
Quick Start
uses
Daf.Hosting,
Daf.Extensions.Hosting,
Daf.Extensions.DependencyInjection;
var Host := THostBuilder.Create
.ConfigureServices(procedure(Context: IHostBuilderContext;
Services: IServiceCollection)
begin
Services.AddSingleton<IMyService, TMyService>;
Services.AddHostedService<TMyWorker>; // IHostedService
end)
.Build;
Host.Start;
Host.WaitForShutdown;IHostedService
Any class registered as a hosted service must implement IHostedService:
type
TMyWorker = class(TInterfacedObject, IHostedService)
public
procedure Start;
procedure Stop;
end;
procedure TMyWorker.Start;
begin
// launched when Host.Start is called
end;
procedure TMyWorker.Stop;
begin
// called on graceful shutdown
end;Register it:
Services.AddHostedService<TMyWorker>;
// equivalent to:
Services.AddSingleton<IHostedService, TMyWorker>;Environment
The environment is read from the DAF_APP_ENV variable (or defaults to Production):
| Value | IsDevelopment | IsStaging | IsProduction | IsTesting |
|---|---|---|---|---|
Development / deve |
โ | |||
Staging / stag |
โ | |||
Production / prod |
โ | |||
Testing / test |
โ |
var Env := Host.Services.GetRequiredService<IHostEnvironment>;
if Env.IsDevelopment then
WriteLn('Running in development mode');
WriteLn(Env.ContentRootPath);Environment variables read at startup:
| Variable | Default | Purpose |
|---|---|---|
DAF_APP_ENV |
Production |
Environment name |
DAF_APP_NAME |
Executable name | Application name |
DAF_CONTENT_ROOT |
App binary path | Content root directory |
Application Lifetime
var Lifetime := Host.Services.GetRequiredService<IHostApplicationLifetime>;
Lifetime.ApplicationStarted.Register(procedure begin
WriteLn('App started');
end);
Lifetime.ApplicationStopping.Register(procedure begin
WriteLn('Stopping...');
end);
// Request a graceful stop from anywhere
Lifetime.StopApplication;Using TDafApplication (Application module)
For console apps, the Application module provides a higher-level wrapper:
uses DAF.Application.Builder;
TDafApplication.CreateHostBuilder
.ConfigureServices(procedure(Context: IHostBuilderContext;
Services: IServiceCollection)
begin
Services.AddSingleton<IMyService, TMyService>;
Services.AddHostedService<TMyWorker>;
end)
.Build
.Run;See Application for the full Application module docs.
Documentation
- ๐ Usage Guide โ configuration pipeline, scoped services, hosted services, lifetime hooks
Abstractions
Hosting.Abstractions
๐ Language: English | Espaรฑol
Core contracts for the DAF hosting system. This module defines interfaces and types only โ no implementation. Reference it when writing hosted services, environment-aware components, or generic host extensions without depending on the concrete host.
Whatโs in this module
All types live in Daf.Extensions.Hosting.
Core interfaces
| Interface | Role |
|---|---|
IHostBuilder |
Fluent builder โ configure and build the host |
IHost |
The running host: Services, Start, Stop, WaitForShutdown |
IHostedService |
Background service contract: Start / Stop |
IHostEnvironment |
Runtime environment: name, app name, paths, IsDevelopment, โฆ |
IHostBuilderContext |
Context passed to configuration callbacks: Environment + Configuration |
IHostApplicationLifetime |
Lifetime signals: ApplicationStarted, ApplicationStopping, ApplicationStopped, StopApplication |
Delegate types
TConfigureHostConfigAction = TProc<IConfigurationBuilder>;
TConfigureAppConfigAction = TProc<IHostBuilderContext, IConfigurationBuilder>;
TConfigureServicesAction = TProc<IHostBuilderContext, IServiceCollection>;Environment constants
TEnvironments.Development // 'Development'
TEnvironments.Staging // 'Staging'
TEnvironments.Production // 'Production'
TEnvironments.Testing // 'Testing'DAF environment variables
TDafEnvVars.APP_ENV // 'DAF_APP_ENV'
TDafEnvVars.APP_NAME // 'DAF_APP_NAME'
TDafEnvVars.CONTENT_ROOT // 'DAF_CONTENT_ROOT'Helper
IServiceCollectionHelper adds AddHostedService<T> directly on IServiceCollection:
Services.AddHostedService<TMyWorker>;
// equivalent to Services.AddSingleton<IHostedService, TMyWorker>Dependencies
Daf.Extensions.DependencyInjection(DependencyInjection.Abstractions)Daf.Extensions.Configuration(Configuration.Abstractions)- Delphi RTL only
The implementation is in Hosting.