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
    ..addSingletonInstance<HostingEnvironment>(hostingEnvironment)
    ..addSingletonInstance<HostEnvironment>(hostingEnvironment)
    ..addSingletonInstance<HostBuilderContext>(hostBuilderContext)
    ..addSingletonInstance<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();

  host_builder.addLifetime(services);
}