McpNetwork.WinNuxService 8.1.0

dotnet add package McpNetwork.WinNuxService --version 8.1.0                
NuGet\Install-Package McpNetwork.WinNuxService -Version 8.1.0                
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="McpNetwork.WinNuxService" Version="8.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add McpNetwork.WinNuxService --version 8.1.0                
#r "nuget: McpNetwork.WinNuxService, 8.1.0"                
#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 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 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. 
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
8.1.0 242 12/8/2023
7.1.0 133 12/8/2023
7.0.0 400 11/13/2022
6.1.0 130 12/8/2023
6.0.0 1,028 11/19/2021
1.1.1 363 5/24/2021
1.0.3 566 5/16/2021 1.0.3 is deprecated because it is no longer maintained.