What is windows service? How to create windows service using .Net?

The main function of windows service is to run the application in the background. Mainly creating service in long running executable applications, in earlier times we called as NT services.

Introduction

Windows service automatically start when the machine boot and also manually we can start and stop. Usually windows service will not have user interface. In windows xp or some other windows os you can view the list of Services currently running on your compure.

Go to Control Panel -> Administrative Tools -> Services

Step 1. Create Windows Service Project

To create a new Window Service, select Windows Service option from your Visual C# New Projects, The Wizard include the WebService1.cs class to your project.The default code of WebService1.cs added by the Wizard looks like here

using

System;

using

System.Collections;

using

System.Core;

using

System.ComponentModel;

using

System.Configuration;

using

System.Data;

using

System.Web.Services;

using

System.Diagnostics;

using

System.ServiceProcess;

namespace

TestWinService

{

publicclassWinService1 : System.ServiceProcess.ServiceBase

{

/// Required designer variable.

private System.ComponentModel.Container components;

public WinService1()

{

// This call is required by the WinForms Component Designer. InitializeComponent();

Step 2. Add your code in Onstart and Onstop function

Step 3: Install and Run the Service

You will get windows service exe, when you build the windows service and we need need to register the service using installutil in command line.

installutil D:\TestWinService \bin\Debug\TestService.exe

Step 4: Uninstall the Service

we need to use /u option to uninstall the service.

installutil /u D:\TestWinService \bin\Debug\TestService.exe

Step 5: Stop and Start

Go to Administrative Tools --> Computer management --> Under Services and Applications, you will see --> TestService.exe. Right click on the TestService.exe with the properties we can stop and start the services.