hosting library
Provides classes for managing application lifetime, hosting background services, and coordinating application startup/shutdown.
This library implements hosting abstractions inspired by Microsoft.Extensions.Hosting, enabling structured application lifecycle management with dependency injection integration.
Basic Application Host
Create and run a hosted application:
final host = createDefaultBuilder(args)
..configureServices((context, services) {
services.addSingleton<MyService>();
})
.build();
await host.run();
Background Services
Run long-lived background tasks:
class MyBackgroundService extends BackgroundService {
@override
Future<void> executeAsync(CancellationToken stoppingToken) async {
while (!stoppingToken.isCancellationRequested) {
// Do background work
await Future.delayed(Duration(seconds: 10));
}
}
}
services.addHostedService<MyBackgroundService>();
Application Lifetime
React to application lifecycle events:
final lifetime = host.services
.getRequiredService<HostApplicationLifetime>();
lifetime.applicationStarted.register(() {
print('Application started');
});
lifetime.applicationStopping.register(() {
print('Application is shutting down');
});
Host Configuration
Configure the application environment and settings:
final host = createApplicationBuilder()
..environment.environmentName = 'Production'
..configuration.addJsonFile('appsettings.json')
..services.addLogging()
.build();
Classes
- ApplicationLifetime
- Allows consumers to perform cleanup during a graceful shutdown.
- BackgroundService
- Base class for implementing a long running HostedService.
-
ConfigureContainerAdapter<
TContainerBuilder> - DefaultHostBuilder
- Environments
- Commonly used environment names.
- Host
- A program abstraction.
- HostApplicationBuilder
- A builder for hosted applications and services which helps manage configuration, logging, lifetime and more.
- HostApplicationBuilderSettings
- Settings for constructing an HostApplicationBuilder.
- HostApplicationLifetime
- Allows consumers to be notified of application lifetime events. This interface is not intended to be user-replaceable.
- HostBuilder
- A program initialization abstraction.
- HostBuilderAdapter
- HostBuilderContext
- Context containing the common services on the Host. Some properties may be null until set by the Host.
- HostDefaults
- Constants for HostBuilder configuration keys.
- HostedService
- Defines methods for objects that are managed by the host.
- HostEnvironment
- Provides information about the hosting environment an application is running in.
- HostingEnvironment
- HostLifetime
- Tracks host lifetime.
- IServiceFactoryAdapter
-
ServiceFactoryAdapter<
TContainerBuilder>
Extensions
- HostEnvironmentEnvExtensions on HostEnvironment
- Extension methods for HostEnvironment.
- HostingAbstractionsHostBuilderExtensions on HostBuilder
- Provides extension methods for the HostBuilder from the hosting abstractions package.
- HostingAbstractionsHostExtensions on Host
- HostingHostBuilderExtensions on HostBuilder
- Provides extension methods for the HostBuilder from the hosting package.
-
OptionsBuilderExtensions
on OptionsBuilder<
TOptions> - ServiceCollectionHostedServiceExtensions on ServiceCollection
- Extension methods for adding hosted services to a ServiceCollection.
Functions
-
createApplicationBuilder(
{HostApplicationBuilderSettings? settings}) → HostApplicationBuilder -
createDefaultBuilder(
[List< String> ? args]) → HostBuilder - Initializes a new instance of the HostBuilder class with pre-configured defaults.
-
createHostingEnvironment(
Configuration hostConfiguration) → HostEnvironment -
populateServiceCollection(
ServiceCollection services, HostBuilderContext hostBuilderContext, HostEnvironment hostingEnvironment, Configuration appConfiguration, ServiceProvider serviceProviderGetter()) → void -
resolveContentRootPath(
String? contentRootPath, String basePath) → String -
resolveHost(
ServiceProvider serviceProvider) → Host
Typedefs
- ConfigureAppConfigurationDelegate = void Function(HostBuilderContext context, ConfigurationBuilder configuration)
-
ConfigureContainerAdapterDelegate<
TContainerBuilder> = void Function(HostBuilderContext hostContext, TContainerBuilder containerBuilder) -
ConfigureContainerDelegate<
TContainerBuilder> = TContainerBuilder Function(HostBuilderContext context, TContainerBuilder builder) - ConfigureHostConfigurationDelegate = void Function(ConfigurationBuilder configuration)
- ConfigureServicesDelegate = void Function(HostBuilderContext context, ServiceCollection services)
- ContextResolver = HostBuilderContext? Function()
-
FactoryResolver<
TContainerBuilder> = ServiceProviderFactory< TContainerBuilder> Function(HostBuilderContext hostContext) -
ServiceProviderFactoryDelegate<
TContainerBuilder> = ServiceProviderFactory< TContainerBuilder> Function(HostBuilderContext? context)