WindowsServiceClient class

Namespace Runic.Platform.Administration.Windows.Services in Runic.Platform.Administration.Windows 0.23.0

public sealed class WindowsServiceClient

Explicit local or remote Service Control Manager access using the current Windows identity.

Remarks

Each call owns its native handles. Configuration and status reads are not an atomic snapshot. Native SCM calls run off the caller's thread; cancellation before a call starts prevents it, and once a call starts its actual result is returned.

Constructors

WindowsServiceClient

public WindowsServiceClient(string? machineName = null)

Creates a client; null targets the local computer. Remote access uses Windows SCM RPC.

Methods

ContinueAsync

public Task ContinueAsync(string name, CancellationToken cancellationToken = default)

Requests continuation of a paused service; use WaitForStateAsync to observe completion.

CreateAsync

public Task CreateAsync(ServiceSpecification specification, string? password = null, CancellationToken cancellationToken = default)

Creates a new own-process service. Supplemental configuration is applied afterward and is not transactional.

Remarks

If a later step fails, the created service remains. Inspect its state before deciding how to recover. Password is never retained.

DeleteAsync

public Task<bool> DeleteAsync(string name, CancellationToken cancellationToken = default)

Marks an existing service for deletion. Returns false only if it was absent; deletion may be deferred by Windows.

EnumerateAsync

public Task<ImmutableArray<ServiceSummary>> EnumerateAsync(CancellationToken cancellationToken = default)

Enumerates Win32 services, including inactive services, without reading privileged configuration.

FindAsync

public Task<ServiceSnapshot?> FindAsync(string name, CancellationToken cancellationToken = default)

Reads service configuration and status; null means the service does not exist. Denied configuration access remains an error.

FindStatusAsync

public Task<ServiceStatus?> FindStatusAsync(string name, CancellationToken cancellationToken = default)

Reads status only; null means the service does not exist.

PauseAsync

public Task PauseAsync(string name, CancellationToken cancellationToken = default)

Requests a pause; use WaitForStateAsync to observe completion.

StartAsync

public Task StartAsync(string name, CancellationToken cancellationToken = default)

Starts a service. Already running is reported as a native conflict.

StopAsync

public Task StopAsync(string name, CancellationToken cancellationToken = default)

Requests a stop; use WaitForStateAsync to observe completion.

StopWithDependentsAsync

public Task<ImmutableArray<string>> StopWithDependentsAsync(string name, TimeSpan timeout, CancellationToken cancellationToken = default)

Stops the active services that depend on a service, in dependency order, waiting for each to stop, and then requests the service's own stop, as ServiceController.Stop() does. Returns the dependents it stopped, in the order they were stopped.

Remarks

Without this, WindowsServiceClient.StopAsync fails with native 1051 (ERROR_DEPENDENT_SERVICES_RUNNING, AdministrationErrorCategory.Conflict) while a dependent runs. timeout bounds the wait for all dependents together and throws TimeoutException; use WaitForStateAsync to observe the service's own stop. Restart the returned dependents in reverse order to restore them. A dependent that starts again meanwhile makes the final stop fail with 1051. Not transactional: dependents stopped before a failure stay stopped.

UpdateAsync

public Task UpdateAsync(string name, ServiceUpdate update, string? password = null, CancellationToken cancellationToken = default)

Updates only supplied fields. Password changes require an explicit AccountName. Native changes are not transactional.

WaitForStateAsync

public Task<ServiceStatus> WaitForStateAsync(string name, ServiceState state, TimeSpan timeout, CancellationToken cancellationToken = default)

Waits for an observed state; timeout throws TimeoutException. Cancellation does not stop or undo service operations.