Create and configure the host¶
| Field | Value |
|---|---|
| Operation ID | OP-02 |
| Trigger | Run the Windows Service program step 1, or a direct consumer call |
| Inputs | args |
| Outputs | A built, configured IHost, also stored in static ServiceHost |
| Primary components | ServiceProgramRunnerBase.Create, DelayStartup (private), Configure, ConfigureLogger |
| Entry point | src/SAIF.NetCore.WindowsService/SAIF.NetCore.WindowsService/SAIF.NetCore.WindowsService/ServiceProgramRunnerBase.cs |
Steps¶
- Build the host builder with
CreateHostBuilder(args)(see Create the default host builder, overridable), then.Build()it and assign the result toServiceHost. - The consumer override normally chains
.ConfigureServiceswith a lambda to callRegisterForWindowsService()andAddHostedService<TWorker>(). - On failure: DI validation errors (when
ValidateOnBuildis enabled in the Development environment) surface here. - Delay startup when configured (WS-BR-02): call
DelayStartup(ServiceHost), which resolvesIConfigurationwithGetService, readsGetValue<int?>("DelayStart"), and callsThread.Sleep(value)if a value is present. - On failure: a non-integer value throws
InvalidOperationExceptionfrom the binder. A negative value other than-1throwsArgumentOutOfRangeException;-1sleeps forever (Thread.Sleep(Timeout.Infinite))*. IfIConfigurationwere unregistered, this would throwNullReferenceException-- but the Generic Host always registers it. - Run the configuration hooks with
Configure(ServiceHost)(WS-BR-04), in order: - Resolve
IWindowsServiceErrorHandlerwithGetRequiredServiceand pass it toConfigureErrorHandler(empty by default; consumers override it to add shutdown-exempt types and special actions).- On failure:
InvalidOperationExceptionif the handler is not registered (WS-BR-05).
- On failure:
- Configure the logger with
ConfigureLogger(host)(WS-BR-05): resolveISaifWindowsServiceLogService,IEnvironmentClient, andIHttpJsonClientwithGetRequiredService, then calllogger.Configure(envClient, httpClient).- Delegates to
SAIF.NetCore.Logging'sSaifWindowsServiceLog.Configure(IEnvironmentClient, IHttpJsonClient): builds the standard logger configuration, readstracingdefaults:loggingapiurlandtracingdefaults:logroutethroughIEnvironmentClient.GetSettingValue(tracingdefaults:logerrorrouteis not read for Windows Services, so the API error route staysnull), computes the EventLog source name fromTrackName/ApplicationName, and replaces the process-global Serilog logger with EventLog, Logging API, and Console sinks. > Origin: This delegation is implemented in OP-02: Configure the Windows Service logger (SaifWindowsServiceLog) (SAIF.NetCore.Logging) and executed on behalf of this operation. - On failure: an exception in
ConfigureErrorHandlerabortsCreatebefore the logger is configured, so that failure is never written to EventLog or the Logging API; it propagates out ofRuntoMain.
- Delegates to
- Return
ServiceHost.
Process flow¶
flowchart TD
Start([Create args]) --> Build[CreateHostBuilder then Build]
Build --> Assign[Assign to ServiceHost]
Assign --> DelayCheck{DelayStart configured?}
DelayCheck -->|Yes| Sleep[Thread.Sleep DelayStart]
DelayCheck -->|No| Hooks
Sleep --> Hooks["Configure: ConfigureErrorHandler"]
Hooks --> Logger["ConfigureLogger: resolve env/http, call logger.Configure"]
Logger --> Return([Return ServiceHost])
Failure and edge-case behavior¶
- A non-integer
DelayStartvalue throwsInvalidOperationException; construction of the host aborts. DelayStart = -1blocks the main thread forever*.- Missing
IWindowsServiceErrorHandler/ISaifWindowsServiceLogService/IEnvironmentClient/IHttpJsonClientregistrations throwInvalidOperationExceptionfromGetRequiredService. - An exception thrown inside the consumer's
ConfigureErrorHandleroverride skips logger configuration and propagates toMain, so the failure is never logged through the SAIF logger.
Related documentation¶
Note: Statements marked with * were inferred from code structure, naming conventions, or contextual evidence where direct confirmation was not available in source artifacts. Statements marked with ** could not be confirmed; treat as unknown until verified.
Sources¶
src/SAIF.NetCore.WindowsService/SAIF.NetCore.WindowsService/SAIF.NetCore.WindowsService/ServiceProgramRunnerBase.cssrc/SAIF.NetCore.Logging/SAIF.NetCore.Logging/SAIF.NetCore.Logging/SaifWindowsServiceLog.cs(logger sink configuration and thetracingdefaults:*reads performed byConfigure)