McpNetwork.DotNetCoreWindowsService
2.0.1
dotnet add package McpNetwork.DotNetCoreWindowsService --version 2.0.1
NuGet\Install-Package McpNetwork.DotNetCoreWindowsService -Version 2.0.1
<PackageReference Include="McpNetwork.DotNetCoreWindowsService" Version="2.0.1" />
paket add McpNetwork.DotNetCoreWindowsService --version 2.0.1
#r "nuget: McpNetwork.DotNetCoreWindowsService, 2.0.1"
// Install McpNetwork.DotNetCoreWindowsService as a Cake Addin #addin nuget:?package=McpNetwork.DotNetCoreWindowsService&version=2.0.1 // Install McpNetwork.DotNetCoreWindowsService as a Cake Tool #tool nuget:?package=McpNetwork.DotNetCoreWindowsService&version=2.0.1
McpNetwork.DotNetCoreWindowsService
This NuGet permits any .Net Core console application to be run as a Windows 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 McpNetWork.DotNetCoreWindowsService;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var serviceManager = new DotNetCoreWindowsService<TestService>(newTestService());
if (System.Diagnostics.Debugger.IsAttached)
{
serviceManager.RunAsConsole(args);
}
else
{
serviceManager.RunAsService();
}
}
}
}
File TestService.cs
using McpNetWork.DotNetCoreWindowsService.Abstracts;
using System;
using System.ServiceProcess;
namespace ConsoleApp1
{
class TestService : ADotNetCoreService
{
public override void Dispose()
{
// This is the right place to dispose your objects.
}
public override void OnStart(string[] args)
{
// insert here the code to start your service
}
public void OnStop()
{
// insert here the code to stop your service
}
}
}
Pausing the service
Service can be paused and resumed 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. For that, override the HandlePowerEvent property and set it to true. You also have to override the OnPowerEvent method.
System shutdown
Service should be notified when the system is shutting down 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. 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
Test
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.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. 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. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- System.Management (>= 4.7.0)
- System.ServiceProcess.ServiceController (>= 4.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.