SelfInstall 1.0.2

dotnet add package SelfInstall --version 1.0.2
                    
NuGet\Install-Package SelfInstall -Version 1.0.2
                    
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="SelfInstall" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SelfInstall" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="SelfInstall" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SelfInstall --version 1.0.2
                    
#r "nuget: SelfInstall, 1.0.2"
                    
#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.
#:package SelfInstall@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=SelfInstall&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=SelfInstall&version=1.0.2
                    
Install as a Cake Tool

SelfInstall

Make Windows Services install themselves

NuGet: https://www.nuget.org/packages/SelfInstall Github: https://github.com/janmarques/SelfInstall

Quickstart

Install the package.

In Program.cs, add the following snippet:

using SelfInstall;
using static SelfInstall.Extensions;

var builder = WebApplication.CreateBuilder(args);

[...]

builder.ConfigureSelfInstall(new SelfInstallOptions
{
    [...]
});

var app = builder.Build();

var didInstallationProcess = await app.RunSelfInstall(args);
if (didInstallationProcess)
{
    return;
}

[...]

app.Run("http://localhost:1991");

When the program runs in interactive mode (ie in a console), it will get installed as a Windows Service and quit. When it runs in non-interactive mode (ie as a Windows Service), it will run normally.

Installing a Windows Service requires admin privileges, so make sure to add this in an app.manifest file:

<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" >
	<assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
	<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2" >
		<security >
			<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3" >
				<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
			</requestedPrivileges >
		</security >
	</trustInfo >
</assembly >

And reference the manifest in your csproj

	<TargetFramework>net8.0-windows</TargetFramework>
	<ApplicationManifest>app.manifest</ApplicationManifest>

Windows Services are (obviously) only available on Windows, hence the package targets -windows.

A full example is available in SelfInstall.WebSample.

Tips / known annoyances

  • Installing/removing services can be tricky and behave strange when observed. Eg when you get an error: The specified service has been marked for deletion. ⇒ close services.msc, task manager and Visual Studio.

  • There is not a lot of logging. Check the Event Viewer.

  • When this package is added and you run it in Visual Studio, it will start installing it. If you want to keep debugging your existing code from the service itself, supply the argument "local", and the installation will be skipped and the program will run as if it were installed You can do this via the launchSettings.json by adding "commandLineArgs": "local"

  • When you configure SelfInstall to copy the exe to a different folder and run it from there, make sure the exe is self-contained (because it is the only file that gets copied, so it won't be able to reach any needed dll's). Use <PublishSingleFile>true</PublishSingleFile> in your FolderProfile.pubxml

Product Compatible and additional computed target framework versions.
.NET net8.0-windows7.0 is compatible.  net9.0-windows was computed.  net10.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.

Version Downloads Last Updated
1.0.2 200 8/8/2024
1.0.1 152 8/7/2024