Woof.ServiceInstaller 5.2.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package Woof.ServiceInstaller --version 5.2.0                
NuGet\Install-Package Woof.ServiceInstaller -Version 5.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Woof.ServiceInstaller" Version="5.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Woof.ServiceInstaller --version 5.2.0                
#r "nuget: Woof.ServiceInstaller, 5.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Woof.ServiceInstaller as a Cake Addin
#addin nuget:?package=Woof.ServiceInstaller&version=5.2.0

// Install Woof.ServiceInstaller as a Cake Tool
#tool nuget:?package=Woof.ServiceInstaller&version=5.2.0                

Woof.ServiceInstaller

.NET extension created by CodeDog

Distributed under MIT License. (c)2021 by CodeDog, All rights reserved.


About

Installs and runs systems services. Works both on Windows and Linux. On Linux uses systemd.

For help use dotnet {command} --help. To install: sudo dotnet {command} --install To uninstall: sudo dotnet {command} --uninstall To delete Windows Event Log: {command} --delete-log (administrator required)

This package uses Woof.CommandLine package to handle the command line arguments with POSIX syntax guidelines.

Usage

Given:

  • ClientService is your service class implementing IHostedService,
  • Config.Data contains your service metadata class instance implementing IConfiguration

Here's example Main mehtod:

private async static Task<int> Main(string[] args) {
    var commandLine = CommandLine.Default; // default CommandLine instance
    ServiceInstaller.Configure<ClientService>(Config.Data); // required
    if (ServiceInstaller.ServiceMetadata is null) throw new NullReferenceException();
    if (args?.Length > 0) { // run only when arguments present (not as service)
        commandLine.Map<Options>(); // optional additional options
        commandLine.Delegates.Add(Options.Help, Help); // optional additional delegates
        commandLine.Parse(args); // required
        var errors = CommandLine.ValidationErrors;
        if (errors is not null) Error(errors); // optionally show errors if any
        else await commandLine.RunDelegatesAsync(); // required
    }
    else
        await ServiceController
            .RunHostAsync<ClientService>(ServiceInstaller.ServiceMetadata!);
            // required
    return 0;
}

To execute code before or after installation / uninstallation simply check if the install/uninstall options are present before and/or after RunDelegatesAsync like this:

if (commandLine.HasOption(ServiceInstaller.Options.Install)) {
    // code to execute
}

The installed service is started with

ServiceController.RunHostAsync<ClientService>(ServiceInstaller.ServiceMetadata!);

If executing of some external commands is needed before or after the installation, ServiceController.ExecAsync method can be used for that. If the command executed that way fails, the method will throw an InvalidOperationException with the error message obtained from the command output.

For more information see the included example project.

Compatibility

The package is compatible with .NET Standard 2.1 and .NET 5.0 or newer. The applications using the module can run both on Windows and Linux.


Disclaimer

Please report any issues to the toolkit developer.

Woof Toolkit is a work in progress in constant development, however it's carefully maintained with production code quality.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

ADD: ServiceController.IsRunningAsync(), ServiceController.QueryAsync().