populateServiceCollection function

void populateServiceCollection(
  1. ServiceCollection services,
  2. HostBuilderContext hostBuilderContext,
  3. HostEnvironment hostingEnvironment,
  4. Configuration appConfiguration,
  5. ServiceProvider serviceProviderGetter(),
)

Implementation

void populateServiceCollection(
  ServiceCollection services,
  HostBuilderContext hostBuilderContext,
  HostEnvironment hostingEnvironment,
  Configuration appConfiguration,
  ServiceProvider Function() serviceProviderGetter,
) {
  services
    ..addSingleton<HostingEnvironment>(
      <HostEnvironment>(_) => hostingEnvironment,
    )
    ..addSingleton<HostEnvironment>(
      <HostEnvironment>(_) => hostingEnvironment,
    )
    ..addSingleton<HostBuilderContext>(
      (_) => hostBuilderContext,
    )
    ..addSingleton<Configuration>(
      <Configuration>(_) => appConfiguration,
    )
    ..addSingleton<ApplicationLifetime>(
      (s) => s.getService<HostApplicationLifetime>() as ApplicationLifetime,
    )
    ..addSingleton<HostApplicationLifetime>(
      (ServiceProvider s) => ApplicationLifetime(
        (s.getRequiredService<LoggerFactory>())
            .createLogger('ApplicationLifetime'),
      ),
    )
    ..tryAdd(
      ServiceDescriptor.singleton<Host>(
        (_) {
          var appServices = serviceProviderGetter();

          return Host(
            appServices,
            appServices.getRequiredService<HostApplicationLifetime>(),
            appServices
                .getRequiredService<LoggerFactory>()
                .createLogger('Host'),
            appServices.getRequiredService<HostLifetime>(),
            appServices.getRequiredService<Options<HostOptions>>(),
          );
        },
      ),
    )
    ..configure<HostOptions>(HostOptions.new, (options) {
      options.initialize(hostBuilderContext.configuration!);
    })
    ..addLogging();

  hostBuilder.addLifetime(services);
}