In this blog, I will be demonstrating the process of converting an existing exe application into a Windows Service using multiple steps - Windows Services creation, launching of the exe in hidden mode when services start, log the information using event log, and also, write the unit tests to test Windows Service.
Let's start with Visual Studio to create one .exe application.
Open VS, go to File > New and select Project. Now, select a new project from the dialog box and create a new .NET Framework console application (for example, Myexe) and add the below logic to test that this exe was called from Windows Service.
- static void Main(string[] args)
- {
- using (var sw = new StreamWriter(args[0]))
- {
- sw.WriteLine("Welcome to Windows service in .net");
- }
- Console.Readkey();
- }
Now, let's create a Windows Service using Visual Studio. So, go to File > New and select Project as “Window Service”. I am creating this project under the same solution, but you can try with a new solution too.
The next step is to add an installer for this Windows Service. Right-click on the blank area and select "Add installer". This installer registers the service to the control manager.
Open ProjectInstaller.Designer.cs => InitializeComponent() to update the service name, display name and description of the service, as shown below. And set the account as LocalSystem; else, it will prompt for username and password during installation.
- private void InitializeComponent()
- {
- this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
- this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
- //
- // serviceProcessInstaller1
- //
- this.serviceProcessInstaller1.Password = null;
- this.serviceProcessInstaller1.Username = null;
- this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
- //
- // serviceInstaller1
- //
- this.serviceInstaller1.ServiceName = "MyWindowservice";
- this.serviceInstaller1.Description = "MyWindowservice Description";
- this.serviceInstaller1.DisplayName = "MyWindowservice";
- this.serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Automatic;
- //
- // ProjectInstaller
- //
- this.Installers.AddRange(new System.Configuration.Install.Installer[] {
- this.serviceProcessInstaller1,
- this.serviceInstaller1});
- }
Now, we will implement the real service where we launch the .exe created in step1 when this Windows Service starts.
- Open your service class(in my case MyWindowService.Designer.cs) and you should be having two default methods - OnStart & OnStop().
- Use event logger to log the Windows Service information.
- Go to the InitializeComponent method and initialize the event logger.
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- components = new System.ComponentModel.Container();
- this.eventLogger = new System.Diagnostics.EventLog();
- ((System.ComponentModel.ISupportInitialize)(this.eventLogger)).BeginInit();
- //
- // SystemManagerService
- //
- this.CanHandleSessionChangeEvent = true;
- this.ServiceName = "MyWindowService";
- ((System.ComponentModel.ISupportInitialize)(this.eventLogger)).EndInit();
- }
- #endregion
- private System.Diagnostics.EventLog eventLogger;

Pouria RiyahiPosted Aug 1, 2020, 7:48 AM
Debug mode work correctly but in service my exe dont run
Ishu JainPosted Apr 20, 2020, 8:05 AM
Is it better to convert console application to windows service or write the new windows service ? Is there any advantage of creating new windows service, in terms of performance of threads.As of now, my console app, has large number of threads in it.
Ubaid KhanPosted Feb 20, 2020, 1:52 AM
I have an error when i start service i.e. windows could not start service on local computer Error 1064 An exception occurred in the service when handling the control request. Please guide me its urgent
Abhay DeshpandePosted Oct 10, 2019, 12:46 AM
Nice article, very helpful for beginners..
Mahesh ChandPosted Sep 30, 2019, 10:25 PM
Nice. Thank you!
Sourav Kumar DasPosted Sep 27, 2019, 11:22 PM
Good Article Bro.
Rajanikant HawaldarPosted Sep 27, 2019, 11:49 AM
very good, nice article