Often in the development cycle it is required to create application that can keep running for a long time and perform required operation. Also application being created should be flexible enough to stop, pause or restart as required. Windows service which was earlier knows as NTservice is designed to do exactly the same.
One disadvantage(not exactly disadvantage) from the develops point of view is, since it does not have any user interface, so you cannot debug the application like a regular .NET application. But don't be disheartened as it can always be debugged as a process. .NET provide us with the option to attach to a process and debug the application, cool enough.
Now let's delve into some real stuff and start with creating a simple windows service that will write to event log at regular interval of time.
Step 1: Open visual studio -> File -> New Project-> Visual C# -> Windows -> select Windows Service project and give some meaningful name.
Step 2: After the project loads, select the Service1.cs file and open it. Click on the Toolbox and drag EventLog and Timer controls.
Step 3: By default the Timer control inherits from the
System.Windows.Forms.Timer and this does not work with windows service(
Timer does not fire with Windows Service). So first thing that we will do is to go to designer file and change the namespace of the Timer control from System.Windows.Forms to
Sytem.Timers.
Also if you open the service1.cs code file, you will see it already has override of Onstart and OnStop method ready for you. These method as the name suggest are fired when service starts and stops.
Step 4: At this point all is set, Now let's go ahead and write some code i.e. (code to write the event log at regular interval)
First we will write the code that we want to happen when service starts
private const string sourceName = "SampleServiceApplicationEventLog"; protected override void OnStart(string[] args)
{
/*First ensure that the source name exist, and if not create one and then write your entry.*/
if(!System.Diagnostics.EventLog.SourceExists(sourceName))
{
System.Diagnostics.EventLog.CreateEventSource(sourceName, "Application");
}
this.eventLog1.Source = sourceName;
this.eventLog1.WriteEntry("Service Started successfully");
/* First enable the Timer to ensure that it gets fired at required interval
Set the interval at which you require the timer to get fired
Attach an event that will be fired at given interval */
this.timer1.Enabled = true;
this.timer1.Interval = 5000D;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(timer1_Elapsed);
}
|
Method that will be fired at regular interval of time.
|
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (!System.Diagnostics.EventLog.SourceExists(sourceName))
{
System.Diagnostics.EventLog.CreateEventSource(sourceName, "Application");
}
this.eventLog1.Source = sourceName;
this.eventLog1.WriteEntry("Service running at given interval successfully");
}
|
Finally the code block to execute when service stops
|
protected override void OnStop()
{
if (!System.Diagnostics.EventLog.SourceExists(sourceName))
{
System.Diagnostics.EventLog.CreateEventSource(sourceName, "Application");
}
this.eventLog1.Source = sourceName;
this.eventLog1.WriteEntry("Service Stopped successfully");
} |
Step 5: At this point the coding stuff is complete. Now let's go ahead and add installer. By default a component class containing two installer is added to windows service project.
Right click on the Service1.cs and open it. Right click on the gray zone and click on the link Add Installer.
This will add two new installer to the project. Open the property window of serviceInstaller1 and change the name of the service to some meaningful name and change the StartType to Automatic.
Open the property window for serviceProcessInstaller1 and change the Account value to LocalSystem.
Step 6: Now we need to go ahead and add the setup project that will be used to install the service.
Solution -> Add New Project - > Other Project Types-> Setup and Deployment -> Visual Studio Installer
Step 6.1: Right click on the SampleServiceInstaller (setup project) and add a new Project Output. A popup window appears, Select the Primary output from the List and click OK button.
Step 6.2: Right click again on the Setup project and follow the step suggested in the image below to open Custom Actions window.
Step 6.3: Right click on the Custom Actions icon and click on AddCustom Action. This will open a popup window as displayed below. Double click on the Application Folder and select the primary output and click OK button.
Step 7: Build the setup project, once the build is success,go ahead and install the service(right click on the setup project and install).

Step 8: At this point installation is complete and now you need to start the service.
Start -> Run ->Services.msc
That is all that you need to create a windows service. Simple isn't it.