This article will demonstrate how to create and install a windows service in .Net.
Windows services are the type of application which allow us to create an auto executed program which runs on a given interval. It can start automatically when system boots. We can set service start type to manual also. In that case we need to manually start it from service manager. We can also start, stop and pause the service if we want. Here in this article I will try to create a simple windows service and will explain it step by step. For more information you can go to MSDN site. Lets start creating a windows service.
First of all let's add a windows service project and name it. For this simply follow the step. Open visual studion and go to File>New project. Click on Visual C#>Windows template and choose Window Service. Name the project and press OK. I named it as BigConceptSampleWindowService. See image for reference.
Once project is added, we can find new service named as Service1. Now its time to rename the service. Rename Service1 with appropriate service name. I renamed it as MySampleWindowService. Below screen will show up when new service project is added. You can see its name as Service1. Just use Find and replace and rename this with your service name.
When we are done with rename, now it's time to add code to the service. If we notice, by default two methods OnStart() and OnStop() is already there in cs file. Let's put some code in these methods. Here I am using Timer so that my service can run on a given interval. Otherwise my code will only execute when service start or stop.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace BigConceptSampleWindowsService
{
public partial class MySampleWindowService : ServiceBase
{
Timer _timer = new Timer();
public MySampleWindowService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
//Put your code here to be executed by service when it is started
_timer.Elapsed+=ExecuteTimer;
_timer.Interval = 60000;//60 seconds. Interval is in milliseconds
_timer.Start();
EventLog.WriteEntry("Service is started at " + DateTime.Now.ToString());
}
private void ExecuteTimer(object sender, ElapsedEventArgs e)
{
//Put your code to be executed at a certain interval
EventLog.WriteEntry("Service is progressing and current time is " + DateTime.Now.ToString());
}
protected override void OnStop()
{
//Put your code here to be executed by service when it get stoped.
EventLog.WriteEntry("Service stoped at " + DateTime.Now.ToString());
}
}
}
I put code to log entry in event log. If we notice, I set timer interval to 60000. It means, my code will execute at an interval of 1 minute. You can set your own interval as per your requirement. If you notice Program.cs file, you will find a set of code in Main() function. This is the code which helps service to run. You will find your service name is pointing here. In my case it is MySampleWindowService().
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MySampleWindowService()
};
ServiceBase.Run(ServicesToRun);
}
}
Now my service is ready and it's time to add installer so that service can be install in service manager. For this I will follow few simple steps. Right click on service and Click on Add Installer.
You will see an installer added to project and is named as ProjectInstaller. You will find two items serviceInstaller1 and serviceProcessInstaller1.
We will set properties of both the items. First click on serviceInstaller1 and click Properties on right panel.Set DisplayName and ServiceName as your service name. In my case it is MySampleWindowService so I am setting it. Now set the StartType. We can set service start type as Automatic or Manual. I left it to Manual.
Now set the properties for serviceProcessInstaller1. Set Account as LocalSystem as it has broder permission. You can set Account as LocalService, NetworkService, LocalSystem or User as per your requirement. I set it to LocalSystem. Also you will not be able to write to Event Log if selected LocalService as Account.
After these simple steps now it is time to build the solution. After successful build we will start installing service to service manager so that it can start executing. I will follow these simple steps to install my service. Open the Developer Command Prompt under Visual Studio Tools. As I am using Visual Studio 2013 so I will open VS 2013 developer command prompt. To open this Right click on VS2013 icon and press Open File Location. File explorer will open. Double click Visual Studio Tools. Right click on Developer Command Prompt for VS2013 and press Run as Administrator. To install or uninstall service I will use installutil.exe command.
To install a service use this command:
To uninstall a service use this command
To install my service I put command like this
Now my service is installed.To view the installed I will go to service manager. For this press Window key+R. Type services.msc and press OK. Service Manager will open where you can find the installed service.
Here I can see my service is installed successfully and its start type is manual. Now I need to start my service to execute. For this I will simply click on Start on left side and my service is started. I can manage my service from here. I can set start type to automatic. I can start, stop or pause my service and so on.
See it was very simple to create. Hope it can help you. I will come with more such articles soon. Till than use this and try yourself.