Archive for the ‘.NET’ Category

Building Windows Service using visual studio-C#

December 5, 2013 Leave a comment

Hi All,
Here I come back again and this time with Windows Service and related things. 🙂
In my carrier I had came across this stuff several times and always thought of writing something on this but unfortunately it always got skipped and went out side of the bucket.
Anyways here I start with:
Windows Service is an windows utility which keeps running behind the screens silently but sometime violently. As this utility runs behind the screen hence its obvious this has nothing to do with the UI, forms or any other cosmetic works.

  • So, how to build it?

Well building an win.service basically one needs to have 2 things:
1. Windows Service Class: Accommodates the code for which the service is written
2. Installer Class: For installing the service

Open visual studio 2010 (the steps are almost same since from ages!) & create an project , while doing so choose the WindowsService Template under the winodws header of your intended programming language.
More details can be retrieved from MSDN, CodeProject

Installing / Unistalling Window Service

      1. Windows service can be installed by firing installutil from the visual studio command prompt – Details on MSDN
      2. Alternative for the installutil is to make use of System.Configuration.Install assembly

using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Reflection;
namespace WinService
static class Program
static void Main(string[] args)
if (Environment.UserInteractive)
string parameter = string.Concat(args);
switch (parameter)
case "--install":
ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
case "--uninstall":
ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
ServiceBase[] servicesToRun = new ServiceBase[]
new Service11()
#region Commented Code -- default code
//static void Main()
// ServiceBase[] ServicesToRun;
// ServicesToRun = new ServiceBase[]
// {
// new Service11()
// };
// ServiceBase.Run(ServicesToRun);

    1. For Deployment purpose we can make use of the installer package as mentioned in these articles MSDN, Torben M. Philippsen
Categories: .NET, General Tags: