An auto-updating Windows service with the ability to send a new compressed version on demand
$ dotnet add package Dzidek.Net.AutoUpgrade.CommonAn auto-updating Windows service with the ability to send a new compressed version on demand

Packages (Dzidek.Net.AutoUpgrade.Service and Dzidek.Net.AutoUpgrade.Upgrader) allow us to implement a service with an automatic update on demand if part of our application is installed outside our servers where we do not have administrator rights or even log in
AutoUpgrade has two services (one with the ".Service" suffix and the other with the ".Upgrader" suffix). The "Service" is responsible for downloading and saving new versions in the directory. The directory where new versions are saved is watched by "Upgrader" and if a new file has been created, it stops "Service", copies new files and starts "Service".
You can control it from the outside when an update happens because it happens when you save a new version and you decide when you give a new version
In appSettings.json:
{
"AutoUpgrade": {
"ServiceName": "AutoUpgrade"
}
}
In program.cs:
builder.Host
.UseAutoUpgradeService(builder.Configuration.GetSection("AutoUpgrade").Get<AutoUpgradeServiceConfiguration>()!);
Remember to set content root path for your windows service
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
});
Below is an example of how to use it through the API over HTTP/HTTPS. You can use it as you want
app.MapGet("/",
(IAutoUpgradeService autoUpgradeService) => $"Hello World from service '{autoUpgradeService.GetVersion()}'!");
app.MapPost("/", (IFormFile file, IAutoUpgradeService autoUpgradeService) =>
{
using var fileStream = new MemoryStream();
file.CopyTo(fileStream);
return autoUpgradeService.Upgrade(fileStream.ToArray(), file.FileName);
});
In appSettings.json:
{
"AutoUpgrade": {
"ServiceName": "AutoUpgrade",
"ServicePath": "[The required path to the directory where the exe file is located]"
}
}
In program.cs:
builder.Host
.UseAutoUpgradeUpgrader(builder.Configuration.GetSection("AutoUpgrade").Get<AutoUpgradeUpgraderConfiguration>()!);
Remember to set content root path for your windows service
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
});
You should call with admin rights. You should first create Service because Upgrader automatically starts Service
sc.exe create "AutoUpgrade.Service" binpath="[PATH]\AutoUpgrade.Service.exe" start=auto
sc.exe create "AutoUpgrade.Upgrader" binpath="[PATH]\AutoUpgrade.Upgrader.exe" start=delayed-auto
The working example is available in the GitHub repository AutoUpgrade.
For testing purposes, you need to build projects and install windows services. You can then send the new version of the service via HTTP to http://localhost:9000. You can use swagger to do this at http://localhost:9000/swagger
Dzidek.Net.AutoUpgrade.Service
Dzidek.Net.AutoUpgrade.Upgrader