McpNetwork.WinNuxService
8.1.0
dotnet add package McpNetwork.WinNuxService --version 8.1.0
NuGet\Install-Package McpNetwork.WinNuxService -Version 8.1.0
<PackageReference Include="McpNetwork.WinNuxService" Version="8.1.0" />
paket add McpNetwork.WinNuxService --version 8.1.0
#r "nuget: McpNetwork.WinNuxService, 8.1.0"
// Install McpNetwork.WinNuxService as a Cake Addin #addin nuget:?package=McpNetwork.WinNuxService&version=8.1.0 // Install McpNetwork.WinNuxService as a Cake Tool #tool nuget:?package=McpNetwork.WinNuxService&version=8.1.0
McpNetwork.WinNuxService
This NuGet permits any .Net (starting version 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 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);
}
}
}
}
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 | 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
- Microsoft.Extensions.Hosting (>= 8.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Hosting.Systemd (>= 8.0.0)
- Microsoft.Extensions.Hosting.WindowsServices (>= 8.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.