Add Installer to Windows service C# Visual Studio 2022

What Is a Windows Service?

A Windows Service is a program that runs in the background. These programs can be configured to start when the operating system starts, or, they can be started manually or by an event.

Creating a Windows Service

Creating a Windows Service is quite easy. Let’s get started.

Start Visual Studio and create a new Windows Service project in either C# or VB.NET. You find this project type under the Windows Desktop section, as shown in Figure 1.

Add Installer to Windows service C# Visual Studio 2022

Figure 1: Windows Service

When the project finishes loading, add the following namespaces:

C#

using System.Diagnostics;
using System.ServiceProcess;
using System.Timers;

VB.NET

Imports System.Diagnostics
Imports System.ServiceProcess
Imports System.Timers

These namespaces help with setting up a timer equipped to handle service events. The service’s functionality comes with the ServiceProcess namespace and the Diagnostics namespace enables us to have a decent Log for all our events from the server.

Add the following variables to keep track of the events in the Windows Event Log.

C#

   private int ID = 1;

VB.NET

   Private ID As Integer = 1
   Private Log As System.Diagnostics.EventLog

Add the Constructor and the OnStart Event.

C#

   public Service_C()
   {

      InitializeComponent();

      Log = new EventLog();

      if (!EventLog.SourceExists("Service_CSource"))
      {

         EventLog.CreateEventSource("Service_CSource",
         "Service_CLog");

      }

      og.Source = "Service_CSource";
      Log.Log = "Service_CLog";

   }

   protected override void OnStart(string[] args)
   {

      Log.WriteEntry("OnStart");

      Timer tmrTime = new Timer();
      tmrTime.Interval = 60000;

      tmrTime.Elapsed += new
         ElapsedEventHandler(this.ElapsedTimer);

      tmrTime.Start();

   }

VB.NET

   Protected Overrides Sub OnStart(ByVal args As String())
      Log = New EventLog()

     If Not EventLog.SourceExists("Service_VBSource") Then
         EventLog.CreateEventSource("Service_VBSource", _
            "Service_VBLog")
      End If

      Log.Source = "Service_VBSource"
      Log.Log = "Service_VBLog"

      Log.WriteEntry("OnStart")
      Dim tmrTime As Timer = New Timer()
      tmrTime.Interval = 60000
      AddHandler tmrTime.Elapsed, AddressOf ElapsedTimer
      tmrTime.Start()
   End Sub

The constructor and the OnStart event create a new Event Log entry and create a new Timer object with its associated ElapsedTimer event. Add this event now.

C#

   protected override void OnStop()
   {

      Log.WriteEntry("OnStop");

   }

   public void ElapsedTimer(object sender, ElapsedEventArgs args)
   {

      Log.WriteEntry("Checking...", EventLogEntryType.Information,
         ID++);

   }

VB.NET

   Protected Overrides Sub OnStop()
      Log.WriteEntry("OnStop")
   End Sub

   Public Sub ElapsedTimer(ByVal sender As Object, ByVal args As _
         ElapsedEventArgs)
      Log.WriteEntry("Checking...", EventLogEntryType.Information, _
         Math.Min(System.Threading.Interlocked.Increment(ID), ID _
         - 1))
   End Sub

The ElapsedTimer event writes an entry to the Event Log with the event ID. The OnStop event simply writes OnStop to the event log.

Adding an Installer to the Service

After you have developed your service, you need to add an installer to it. This is so that you can install it on a machine, typically a server. To add an installer, go to the design view of the service, then right-click and select Add Installer, as you can see in Figure 2.

Add Installer to Windows service C# Visual Studio 2022

Figure 2: Add Installer

The Installers are now added to the Service (see Figure 3).

Add Installer to Windows service C# Visual Studio 2022

Figure 3: Installers Added

The work is not done yet!

You now need to set the Properties for the serviceProcessInstaller and serviceInstaller so that the service can be installed correctly on the client. Set the Properties, as shown in Figures 4 and 5.

Add Installer to Windows service C# Visual Studio 2022

Figure 4: Service Installer

Add Installer to Windows service C# Visual Studio 2022

Figure 5: Service Process Installer

Build the project.

Installing the Service

To install the service, you need to open the Command Prompt (see Figure 6) and run it as an Administrator. Then, use the following command:

Installutil YourServiceName.exe

Keep in mind that you have to be in the debug or release folder of your Service, so, if necessary, you will need to use the CD command to change the directory.

Add Installer to Windows service C# Visual Studio 2022

Figure 6: Command Prompt

Conclusion

Windows Services are easy to set up and get started with. I hope this article has helped you do just that.