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.

Delphi 12+ License


Why use this?

  • ๐Ÿ—๏ธ Unified startup โ€” One place for DI, configuration, logging and background services
  • ๐ŸŒ Environment-aware โ€” Built-in Development / Staging / Production / Testing modes
  • โš™๏ธ Layered configuration โ€” Host config, then app config, with environment variable overrides
  • ๐Ÿ”„ Hosted services โ€” Register IHostedService implementations that start and stop with the host
  • ๐Ÿ›‘ Graceful shutdown โ€” IHostApplicationLifetime signals Started, Stopping, Stopped
  • ๐Ÿ”Œ DI integration โ€” IServiceProvider is the backbone; all services registered via IServiceCollection

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.

Delphi 12+ License


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.