McpNetwork.WinNuxService
6.0.0
See the version list below for details.
dotnet add package McpNetwork.WinNuxService --version 6.0.0
NuGet\Install-Package McpNetwork.WinNuxService -Version 6.0.0
<PackageReference Include="McpNetwork.WinNuxService" Version="6.0.0" />
paket add McpNetwork.WinNuxService --version 6.0.0
#r "nuget: McpNetwork.WinNuxService, 6.0.0"
// Install McpNetwork.WinNuxService as a Cake Addin #addin nuget:?package=McpNetwork.WinNuxService&version=6.0.0 // Install McpNetwork.WinNuxService as a Cake Tool #tool nuget:?package=McpNetwork.WinNuxService&version=6.0.0
McpNetwork.WinNuxService
This NuGet permits any .Net 6 console application to be run as a Windows or Linux service.
How to
- Create a new .Net Core console application
- Add a reference to this NuGet
- Update Program.cs as indicated herebelow
- Create TestService.cs file
- Compile the project
- Create the Windows Service
- Test & Enjoy
File Program.cs
using System;
using System.Diagnostics;
using System.Linq;
using McpNetwork.WinNuxService;
namespace DemoService
{
class Program
{
static void Main(string[] args)
{
var serviceManager = new WinNuxService<TestService>();
// var serviceManager = new WinNuxService<TestService>(new TestService());
// Don't forget to keep starting arguments
serviceManager.StartArguments = args;
var isService = !(Debugger.IsAttached || args.Contains("--console"));
if (isService)
{
serviceManager.RunAsService();
}
else
{
serviceManager.RunAsConsole();
}
}
}
}
File TestService.cs
using System;
using System.IO;
using System.ServiceProcess;
using McpNetwork.WinNuxService.Abstracts;
namespace DemoService
{
internal class TestService : AWinNuxService
{
private const string ServiceName = "DemoService";
private readonly string LogFilePath = @"d:\Temp\TestService.log";
public TestService()
{
this.DebugStopKey = ConsoleKey.Z; // Override default CTRL+C to stop service in debug mode
if (OperatingSystem.IsLinux())
{
this.LogFilePath = "/home/pi/Desktop/TestService.log";
}
}
//public override bool HandleShutdown => false;
//public override bool HandlePauseAndContinue => true;
//public override bool HandlePowerEvent => false;
//public override bool HandleSessionChangeEvent => false;
public override void OnContinue()
{
this.Log("OnContinue");
}
public override void OnPause()
{
this.Log("OnPause");
}
public override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
this.Log("OnPowerEvent");
return true;
}
public override void OnShutdown()
{
this.Log("OnShutdown");
}
public override void OnSessionChange(SessionChangeDescription changeDescription)
{
throw new NotImplementedException();
}
public override void OnStart(string[] args)
{
var parameters = new StringBuilder();
foreach(var startArgument in this.StartArguments)
{
parameters.AppendFormat("[{0}] ", startArgument);
}
this.Log(String.Format("OnStart. Parameters : {0}", parameters.ToString()));
}
public override void OnStop()
{
this.Log("OnStop");
}
public void Log(string logLine)
{
const string OUTPUT_FORMAT = "yyyy-MM-dd HH:mm:ss.ffff";
var outputLine = String.Format("{0}|{1}|{2}", DateTime.Now.ToString(OUTPUT_FORMAT), TestService.ServiceName, logLine);
Console.WriteLine(outputLine);
if (!File.Exists(LogFilePath))
{
using var sw = File.CreateText(LogFilePath);
sw.WriteLine(outputLine);
}
else
{
using var sw = File.AppendText(LogFilePath);
sw.WriteLine(outputLine);
}
}
}
}
Pausing the service
Service can be paused and resumed (only on Windows environment). For that, overrride the HandlePauseAndContinue property and set it to true. You also have to override the OnPause and OnContinue methods.
Computer power status change
Service can handle notifications of computer power status changes (only on Windows environment). For that, override the HandlePowerEvent property and set it to true. You also have to override the OnPowerEvent method.
System shutdown
Service can be notified when the system is shutting down (only on Windows environment). For that, override the HandleShutdown property and set it to true. You also have to override the OnShutdown method.
Session change event
Service can handle session change events received from a Terminal Server session (only on Windows environment). For that, you have to override the HandleSessionChangeEvent property and set it to true. You also have to override the OnSessionChange method to manage this event.
Create the Windows Service
The windows service is created using sc
Syntax: sc create TestService binpath= TestService.exe
Create the Linux Service
Refer to linux documentation on creating a linux daemon (you can get a good example on https://swimburger.net/blog/dotnet/how-to-run-a-dotnet-core-console-app-as-a-service-using-systemd-on-linux)
Test
Windows service
You can now open the Service Manager and start, pause, resume and start your service. You can also use NET or SC commands to manage your service.
Linux service
Refer to https://swimburger.net/blog/dotnet/how-to-run-a-dotnet-core-console-app-as-a-service-using-systemd-on-linux for starting a linux service
Product | Versions 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 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. |
-
net6.0
- Microsoft.Extensions.Hosting (>= 6.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Hosting.Systemd (>= 6.0.0)
- Microsoft.Extensions.Hosting.WindowsServices (>= 6.0.0)
- System.ServiceProcess.ServiceController (>= 6.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.