Create Window service project in C# - Part 1 (Create and launch)

Windows service is a running executable on windows which perform a specific functions without user interaction. So you can even create a windows service to do a specific function without your interaction. There is an easy way to create a windows service application in C#. This article describe step by step of creating windows service application in C#.net. and my next parts will describe further more about...

Step 1
Open visual studio and File--> New --> Project. then select "Windows service" and type name and location of your project.


Step 2
Then go to "Service1.cs" file's design view. Then Properties of Service1 and change the 'Name' and 'Service name' as you wish. I changed those as "MyService" as you can see on following screenshot. Then click "Add Installer" to add an installer for this service.

Step 3
Then click on 'serviceInstaller1' component on 'ProjectInstaller.cs' file's design view then get its properties. Now you can change the properties of serviceInstaller1 as shown in following figure.

Make sure the Service name is equal to the service name that you enter for Service1.cs's properties and select StartType as Automatic. Then let's see properties for serviceProcessInstaller1 on ProjectInstaller.cs. You should change the Account as LocalSystem. It will prevent asking username and passward from the user when install this windows service.

Step 4
Now let's see how to get notified whether our service is running or not. For that you can use EventLog component as describe as followings. First go to Service1's design view and the add a eventlog component from tool box.



I changed added event log's name as 'myEventLog'. So then move to the coding... (switch to the code view of Service1.cs file)
Add following code segment to immediately following to the initialize component method calling on the constructor of the MyService class.

 if (!System.Diagnostics.EventLog.SourceExists("MyLogSrc"))
 {
    System.Diagnostics.EventLog.CreateEventSource("MyLogSrc", "MyLog");
 }

 //set our event log to system created log
 myEventLog.Source = "MyLogSrc";
 myEventLog.Log = "MyLog";


This will create a system event log and bind our event log to created log.
Then add following code segment to the override method of OnStart. There are more override methods which are on ServiceBase class as our class extends the ServiceBase class. You can try those methods also for further more actions.

 myEventLog.WriteEntry("MyService started");


OnStart override method can be use to execute actions when our service starts. Because OnStart method executes when a Start command is sent to the service by the Service Control Manager (SCM) or when the operating system starts (for a service that starts automatically).

Let's try OnStop method also. So enter following code segment in OnStop method.

 myEventLog.WriteEntry("MyService stoped");


Now your code should like this,


Now our event logger part finished. You can build this project by pressing F6. (If you try to run this project you will receive an error message which notify you "cannot run or debug... first install this service.." like that) So let's move to install our service on windows.

Step 4
To install the service first create an installation project for our project. Let's create it...
Right click on your solution (not project) from solution explorer window and point to the Add then click on New Project...


Then select Setup Project from the list which can be found under Setup and Deployment category. Then type name for your project then Ok.


Now you can see the new project on solution explorer, right click on it and point to Add then click on Project Output... . Then select your project name and select Primary Output.


Then again right click on setup project and point to View then click on Custom Actions. You will see the custom action window. Then right click on Custom Actions then Add Custom Action . You will see the Select Items from Project window. Select Application folder from the Lock in combo box and select Primary output from (active) then Ok.


No you can see, it will add Primary output for all four actions (Install, Commit, Rollback, and Uninstall).


Now we are ready to install our new windows service on windows. Just right click on setup project and select Install from the drop down menu. Now you will see the installation wizard just like normal installation.

That's all for creating basic Windows and installing windows application in C#.net. You can see your service on windows service list. Just go to Computer management by selecting it from start menu. And navigate to Services which can be find under  Services and application category.


Complete Visual studio C# project can be download from here.

My next post will describe how to check your event log to ensure your program.

8 comments: