Dzidek.Net.AutoUpgrade.Service 1.0.5

dotnet add package Dzidek.Net.AutoUpgrade.Service --version 1.0.5
NuGet\Install-Package Dzidek.Net.AutoUpgrade.Service -Version 1.0.5
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="Dzidek.Net.AutoUpgrade.Service" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dzidek.Net.AutoUpgrade.Service --version 1.0.5
#r "nuget: Dzidek.Net.AutoUpgrade.Service, 1.0.5"
#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 Dzidek.Net.AutoUpgrade.Service as a Cake Addin
#addin nuget:?package=Dzidek.Net.AutoUpgrade.Service&version=1.0.5

// Install Dzidek.Net.AutoUpgrade.Service as a Cake Tool
#tool nuget:?package=Dzidek.Net.AutoUpgrade.Service&version=1.0.5

Windows service with auto upgrade feature

An auto-updating Windows service with the ability to send a new compressed version on demand C1 and sequence diagram

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

How it works

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

Basic usage

Service

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);
});
Configuration
  • ServiceName - service name - should be the same in "Service" and "Updater", both add suffixes to distinguish the two services
  • NewVersionDirectoryName - name of the directory where new versions will be saved. This directory will be watched by the Upgrader. This directory will be placed in the service directory
    • Default value: NewVersion
  • ServiceNameSuffix - service suffix name
    • Default value: Service
    • Example: In the example above, the service name will be "AutoUpgrade.Service"

Upgrader

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
});
Configuration
  • ServiceName - service name - should be the same in "Service" and "Updater", both add suffixes to distinguish the two services
  • ServicePath - the required path to the directory where the exe file is located
  • ServiceNameSuffix - service suffix name
    • Default value: Service
    • Example: In the example above, the service name will be "AutoUpgrade.Service"
  • UpgraderNameSuffix - upgrader suffix name
    • Default value: Upgrader
    • Example: In the example above, the service name will be "AutoUpgrade.Upgrader"
  • ServiceOldVersionsPath - if set, before upgrade current version will be zipped and copied to the directory

Create service in windows

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

Working example

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

Changelog

  • 1.0.5
    • Prevent start service when package exists to upgrade
  • 1.0.4
    • Possibility to zip and keep old versions by setting ServiceOldVersionsPath props in upgrader
  • 1.0.3
    • Unzipping files with subdirectories
  • 1.0.2
    • Removed unnecessary dependencies
  • 1.0.1
    • DotNet6 Fix
  • 1.0.0
    • First version

Nuget packages

Service

Dzidek.Net.AutoUpgrade.Service

Upgrader

Dzidek.Net.AutoUpgrade.Upgrader

Common

Dzidek.Net.AutoUpgrade.Common

Authors

License

MIT

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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.

Version Downloads Last updated
1.0.5 3,643 4/16/2023
1.0.5-alpha4 145 4/16/2023
1.0.5-alpha2 135 4/16/2023
1.0.5-alpha1 145 4/16/2023
1.0.4 1,758 1/26/2023
1.0.3 283 1/26/2023
1.0.2 286 1/24/2023
1.0.1 274 1/24/2023
1.0.0 281 1/24/2023