In this article I will be focusing on the steps for creating your first windows service using a console applictation.You can find many more articles on the same ,but here I will be showing the steps to create windows service quickly when your project needs it urgently and you don't have time to go through lengthy articles.

Hope this article helps you to create the windows service in a very short time and meet your project deadlinesJ

Following are the steps for creating the windows service:

Step 1 :
Add a new Console Application in C# :

Step 2 :

Add reference the following dlls :

  • System.Configuration.Install
  • System.ServiceProcess

Step 3 :

Add the following code to the Program.cs file:

[sourcecode language="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
namespace ServiceExample
{
public class Program : ServiceBase
{
public Program()
{
this.ServiceName = "My Service";
}
static void Main(string[] args)
{
ServiceBase.Run(new Program());
}
protected override void OnStart(string[] args)
{
base.OnStart(args);
}
protected override void OnStop()
{
base.OnStop();
}
}
}
[/sourcecode]

Step 4 :

Add a Component Class and name it as "MyServiceInstaller.cs"

Step 5 :

Add the following code to the MyServiceInstaller.cs file:

[sourcecode language="csharp"]
namespace ServiceExample
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
/// <summary>
/// RunInstaller MyServiceInstaller
/// </summary>
[RunInstaller(true)]
public class MyServiceInstaller : Installer
{
/// <summary>
/// Initializes a new instance of the <see cref="MyServiceInstaller"/> class.
/// </summary>
public MyServiceInstaller()
{
var processInstaller = new ServiceProcessInstaller();
var serviceInstaller = new ServiceInstaller();
////set the privileges
processInstaller.Account = ServiceAccount.LocalSystem;
serviceInstaller.DisplayName = "My Service";
serviceInstaller.StartType = ServiceStartMode.Manual;
////must be the same as what was set in Program's constructor
serviceInstaller.ServiceName = "My Service";
this.Installers.Add(processInstaller);
this.Installers.Add(serviceInstaller);
}
}
}
[/sourcecode]

Step 6 :

Delete the MyServiceInstaller.Designer.cs file as shown below

Step 7 :

Build the solution and now it is ready to be installed.

Step 8 :

  • Goto the command prompt
  • Type the following line :
    [sourcecode language="csharp"] C:\> cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 [/sourcecode]
  • Type the following line :
    [sourcecode language="csharp"] C:\Windows\Microsoft.NET\Framework\v4.0.30319>installutil <projectlocation>\ServiceExample.exe[Name of the exe][/sourcecode]

After this the service is successfully installed.Now we need to start the service.

Step 9 :

  • Goto services.msc.
  • Now you will find your service and then select and start the same.

  • Now manually start the service and Go to "LogOn" tab and select "This Account" and give the appropriate credentials.

Following the above steps successfully completes the installation of your windows service.

Note # :

If you face a security issue while installing the service, the open Visual Studio Command Prompt from Run As Administrator.

After this follow steps 8 & 9 to complete the installation of the service.

Thank You :)