Skip to content

Run one task iteration

Field Value
Operation ID OP-06
Trigger Execute the worker loop step 2
Inputs stoppingToken
Outputs None on success; task-runner exceptions are caught and delegated to the error handler, which may itself throw and propagate out of this operation
Primary components ServiceWorker<T>.RunTaskAsync
Entry point src/SAIF.NetCore.WindowsService/SAIF.NetCore.WindowsService/SAIF.NetCore.WindowsService/ServiceWorker.cs

Steps

  1. Invoke the consumer task with await TaskRunner.Run(stoppingToken).
  2. Delegation -> consumer code implementing SAIF.NetCore.ServiceContracts's IServiceTaskRunner.Run(CancellationToken). ServiceContracts defines no steps of its own; the consumer supplies the business logic.
  3. Catch every exception (catch (Exception ex)) and call await HandleTaskError(ex) (see Handle a task error) (WS-BR-08).
  4. Branch: an OperationCanceledException thrown by a runner that honors stoppingToken during shutdown is also treated as a task error — it is logged as an error and, unless exempted, triggers another StopAsync*.
  5. Branch: HandleTaskError itself is not guarded by a further try/catch here — an exception raised while evaluating the exemption predicate, building the message, calling ErrorHandler.HandleError, or calling ServiceHost.StopAsync propagates out of RunTaskAsync and out of the worker's ExecuteAsync; it is not absorbed (see Handle a task error failure branches).

Process flow

flowchart TD
    Start([RunTaskAsync]) --> Invoke[await TaskRunner.Run stoppingToken]
    Invoke --> Threw{Exception thrown?}
    Threw -->|No| Done([Return normally])
    Threw -->|Yes| Handle[HandleTaskError ex]
    Handle --> HandleThrew{HandleTaskError threw?}
    HandleThrew -->|No| Done
    HandleThrew -->|Yes| Propagate(["Exception propagates out of RunTaskAsync and ExecuteAsync"])

Failure and edge-case behavior

  • A NullReferenceException from a null TaskRunner (see Construct a service worker) is caught here, like any other exception.
  • A cooperative-cancellation OperationCanceledException during shutdown is treated identically to an unexpected error and may trigger an additional StopAsync call, even though the host is already stopping*.
  • An exception thrown inside HandleTaskError is not absorbed by this operation; it propagates out of RunTaskAsync and the worker's ExecuteAsync, skipping whatever shutdown step would otherwise have run.

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/ServiceWorker.cs