McpNetwork.WinNuxService 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package McpNetwork.WinNuxService --version 1.0.3                
NuGet\Install-Package McpNetwork.WinNuxService -Version 1.0.3                
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="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add McpNetwork.WinNuxService --version 1.0.3                
#r "nuget: McpNetwork.WinNuxService, 1.0.3"                
#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=1.0.3

// Install McpNetwork.WinNuxService as a Cake Tool
#tool nuget:?package=McpNetwork.WinNuxService&version=1.0.3                

McpNetwork.WinNuxService

This NuGet permits any .Net 5 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 isService = !(Debugger.IsAttached || args.Contains("--console"));

			var serviceManager = new WinNuxService<TestService>();

			if (isService)
			{
				serviceManager.RunAsService();
			}
			else
			{
				serviceManager.RunAsConsole(args);
			}

		}
	}
}

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)
		{
			this.Log("OnStart");
		}

		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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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. 
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 248 12/8/2023
7.1.0 137 12/8/2023
7.0.0 402 11/13/2022
6.1.0 132 12/8/2023
6.0.0 1,030 11/19/2021
1.1.1 365 5/24/2021
1.0.3 568 5/16/2021 1.0.3 is deprecated because it is no longer maintained.