RuntimeUpgradeNotifier 1.0.0-beta1
dotnet add package RuntimeUpgradeNotifier --version 1.0.0-beta1
NuGet\Install-Package RuntimeUpgradeNotifier -Version 1.0.0-beta1
<PackageReference Include="RuntimeUpgradeNotifier" Version="1.0.0-beta1" />
paket add RuntimeUpgradeNotifier --version 1.0.0-beta1
#r "nuget: RuntimeUpgradeNotifier, 1.0.0-beta1"
// Install RuntimeUpgradeNotifier as a Cake Addin #addin nuget:?package=RuntimeUpgradeNotifier&version=1.0.0-beta1&prerelease // Install RuntimeUpgradeNotifier as a Cake Tool #tool nuget:?package=RuntimeUpgradeNotifier&version=1.0.0-beta1&prerelease
RuntimeUpgradeNotifier
Receive notifications when the .NET Runtime running your process gets upgraded to a new version, so you can restart your process and avoid crashing later.
Requirements
- .NET Runtime 8 or later
Installation
dotnet add package RuntimeUpgradeNotifier
Usage
By default, this library will only notify you when the .NET Runtime is upgraded instead of starting or stopping any processes, but you can choose to have it automatically start or stop your process as well.
Automatically restart process
When the .NET Runtime for your process is upgraded, this will start a new instance of your program with the same command-line arguments, environment variables, and working directory as the current process. It will then exit the old process with status code 1.
using RuntimeUpgrade.Notifier;
using RuntimeUpgrade.Notifier.Data;
RuntimeUpgradeNotifier.RestartBehavior = RestartBehavior.AutoRestartProcess;
RuntimeUpgradeNotifier.RuntimeUpgraded += (_, evt) => {
Console.WriteLine($"Restarting process with PID {evt.NewProcessId}");
return ValueTask.CompletedTask;
};
Custom exit code
By default, the current process will exit with status code 0 as soon as the RuntimeUpgraded
event handler returns. You can change this value with CurrentProcessExitCode
.
RuntimeUpgradeNotifier.RuntimeUpgraded += (_, evt) => {
evt.CurrentProcessExitCode = 1;
return ValueTask.CompletedTask;
};
Automatically start new process and manually exit old process
This allows you to shut down your application more gracefully than Environment.Exit()
can.
RuntimeUpgradeNotifier.RestartBehavior = RestartBehavior.AutoStartNewProcess;
RuntimeUpgradeNotifier.RuntimeUpgraded += (_, evt) => {
/* WPF: */ Application.Current.Shutdown();
/* Forms: */ Application.Exit();
/* .NET Host: */ hostApplicationLifetime.StopApplication();
return ValueTask.CompletedTask;
};
Manually restart process (notify only)
This grants you full customization of the shutdown and restart of your program. For example, you can help your process resume its execution by transferring state using extra command-line arguments, environment variables, IPC messages, or any other signaling technique.
RuntimeUpgradeNotifier.RestartBehavior = RestartBehavior.Manual; //default
RuntimeUpgradeNotifier.RuntimeUpgraded += (_, evt) => {
Process.Start(Environment.ProcessPath, Environment.GetCommandLineArgs().Skip(1).Concat(["--restarted-after-runtime-upgrade"]));
Application.Current.Shutdown();
};
Service restart
If you have a background service or daemon, you can configure Windows or systemd to automatically restart your program when it exits with a non-zero status code.
- Windows:
- GUI:
services.msc
→ service properties → Recovery → ChooseRestart the service
for "First failure", "Second failure", and "Subsequent failures" - CLI:
sc failure MyService actions= restart/0/restart/0/restart/0 reset= 86400
- GUI:
- systemd:
[Service] Restart=on-failure
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- No dependencies.
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.0-beta1 | 57 | 5/30/2024 |