Creating a Simple Windows Service in C#
By FoxLearn 12/26/2024 7:56:35 AM 2
These services can start automatically when the system boots up and continue running even when no user is logged in. In this tutorial, we'll go over the steps to build a Windows Service in C# .NET.
1. Create a Console Application
The first step is to create a new Console Application in Visual Studio.
In Visual Studio, go to File > New > Project, then choose Console Application. Name it according to your preference (e.g., MyWindowsService
).
2. Add Required References
To develop Windows Services, we need to use classes from the System.ServiceProcess
and System.Configuration.Install
namespaces. So, add these references to your project by right-clicking on References in the Solution Explorer, selecting Add Reference, and adding System.ServiceProcess
and System.Configuration.Install
.
3. Modify the Program Class
By default, Visual Studio creates a file named Program.cs
with a basic template. We'll modify this file to make it inherit from System.ServiceProcess.ServiceBase
.
using System; using System.ServiceProcess; namespace MyWindowsService { class Program : ServiceBase { static void Main(string[] args) { ServiceBase.Run(new Program()); } public Program() { this.ServiceName = "My Windows Service"; } protected override void OnStart(string[] args) { base.OnStart(args); // Code to start the service (e.g., initializing variables, starting threads) } protected override void OnStop() { base.OnStop(); // Code to stop the service (e.g., cleaning up, stopping threads) } } }
In this example:
- Inheriting from
ServiceBase
: This gives our class access to essential service functionality. - Overriding
OnStart
andOnStop
: These methods are called when the service starts and stops, respectively. You’ll place your custom logic in these methods.
4. Add an Installer for the Service
Windows Services need to be installed to run properly. To do this, we need to add an installer class that can define how the service should be installed. Let's create a class named MyWindowsServiceInstaller
.
using System; using System.Configuration.Install; using System.ComponentModel; using System.ServiceProcess; namespace MyWindowsService { [RunInstaller(true)] public class MyWindowsServiceInstaller : Installer { public MyWindowsServiceInstaller() { var serviceProcessInstaller = new ServiceProcessInstaller(); var serviceInstaller = new ServiceInstaller(); // Set service privileges serviceProcessInstaller.Account = ServiceAccount.LocalSystem; // Set service details serviceInstaller.DisplayName = "My Windows Service"; serviceInstaller.StartType = ServiceStartMode.Manual; serviceInstaller.ServiceName = "My Windows Service"; this.Installers.Add(serviceProcessInstaller); this.Installers.Add(serviceInstaller); } } }
In this installer class:
- ServiceProcessInstaller: Defines the privileges and account under which the service will run. In this case, we use
LocalSystem
which gives the service administrative access. - ServiceInstaller: Defines the specific details of the service, including its name, start type, and display name.
5. Implement the Main Method
The Main
method is where the service is actually run when executed.
class Program : ServiceBase { static void Main(string[] args) { ServiceBase.Run(new Program()); } public Program() { this.ServiceName = "My Windows Service"; } protected override void OnStart(string[] args) { base.OnStart(args); // Your code to start the service goes here. } protected override void OnStop() { base.OnStop(); // Your code to stop the service goes here. } }
This ensures that when the application is executed, it runs as a service by calling ServiceBase.Run(new Program())
.
6. Install the Service
Before installing the service, you need to build your project in Release Mode.
Afterward, you can install the service using installutil.exe
, which is a utility from Microsoft designed for this purpose.
Open a Command Prompt and navigate to your Release directory where the executable (MyWindowsService.exe
) is located.
Run the following command to install the service:
installutil MyWindowsService.exe
Once installed, you can open the Services Manager (press Win + R
, type services.msc
, and hit Enter), where you should see your service listed as "My Windows Service."
7. Uninstall the Service
If you ever need to uninstall the service, you can do so by running:
installutil -u MyWindowsService.exe
This will remove the service from your system.