Home > .NET, General > Building Windows Service using visual studio-C#

Building Windows Service using visual studio-C#


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 });
break;
case "--uninstall":
ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
else
{
ServiceBase[] servicesToRun = new ServiceBase[]
{
new Service11()
};
ServiceBase.Run(servicesToRun);
}
}
#region Commented Code -- default code
//static void Main()
//{
// ServiceBase[] ServicesToRun;
// ServicesToRun = new ServiceBase[]
// {
// new Service11()
// };
// ServiceBase.Run(ServicesToRun);
//}
#endregion
}
}

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: