Today I am going to share a tutorial on how to write, install and run a Windows Service.
I am using Visual Studio 2013 on Windows 8.1
Lets get started!
STEP 1 : Open a Windows Service project in Visual Studio 2013 and name it as SampleWindowsService.
STEP 2 : Install the installer file. This step is important because you define the start type of your service here to manual or automatic. To install the installer files, right click on the Service1.cs[Design] file and click on "add Installer " . I struggled a lot to do this step correctly because as soon as I landed on the design page I clicked on "click here to switch to code view" and closed the design page. Anyways, as soon as the you have added the installer two files will appear on the ProjectInstaller.cs[Design] file : serviceProcessInstaller1 and serviceInstaller1.
STEP 3 : Right click on serviceInstaller1 file and click on properties. Two properties are of interest to us. First is, StartType and second is ServiceName. On the start type you can specify weather, upon installation you want the service to start automatically or you want to manually start it. I am choosing manual as my StartType for this tutorial. Also make sure that your actual service name is what is reflected in ServiceName in this property windows. For example I am changing the name of my service from Service1 to SampleServic and than I will change the name in the properties of serviceInstaller.
STEP 4 : Now we are ready to write the code for the service. Open SampleService.cs file - specify all what your service does here. You will see two methods over there - OnStart() and OnStop(). Define your methods below OnStop() method and call them in OnStart() method. When have written all the code, build the solution.
STEP 5 : Assuming you have filled MyMethod with meaning code. Now lets try to install the service and see it running. Windows Service runs in the background and accepts no input parameters, which means you cannot F5 and run the service, you will have to install it manually. To do so goto start screen -> all apps -> type visual. You will see a folder named Visual Studio Tools. Open that folder and you will see many command prompts. Launch the Developer Command Prompt for VS2013 in administrative mode.
STEP 6 : Navigate the command prompt to the directory which contains the .exe file of your service. It will be in the bin->debug folder of your Visual Studio Project. The path for my .exe file is this : C:\Users\sakhare\Documents\Visual Studio 2013\Projects\SampleWindowsService\SampleWindowsService\bin\Debug
Once you are in this folder in your command prompt use "InstallUtil"command to install the service.
The command prompt may ask your for Username and password. Fill your domain name/ PC user name and password.
STEP 7 : Now your service is installed. To see it running goto control Panel -> Administrative tools ->Computer management -> services and applications -> services.
On the right side now search for SampleService - this the name we have written for our service in visual studio. click on "start' (left) and see your service running!
Good Luck for writing your Windows Service!
If you find this post useful, please rate/comment and let me know your thoughts and topics you want me to cover. :)