Saturday, December 29, 2012

Create Simple Window Service And Setup Project With Installation

Create Simple Window Service
Open Visual Studio 10 go to File > New > Project, open a dialog box and new project as a “Window Service” and on OK button.

   Add New Project   

   Select Project Type

After created project you can see service design page like below and click on link “click here to switch to code view”

Solution Explorer look like this:     


WinService.cs class should  contains  two functions OnStart() & OnStop() & contain below code:-   public partial class WinService : ServiceBase
        public WinService()
        protected override void OnStart(string[] args)
        protected override void OnStop()

Go to solution explorer and right click on WinService.cs  go to view designer  look design view after that right click on design page and click on “add installer” look like this:

After added ProjectInstaller.cs and you have 2 components on the design view of the ProjectInstaller.cs (serviceProcessInstaller1 and serviceInstaller1). You should then setup the properties as you need.: 
       >Click on serviceinstaller1 and go to property right pane and edit service name Service1 to WinService, look like:

         >Click on serviceProcessInstaller1 and go to property right pane and select account & choose LocalService from dropdown and save. See below:

You have two assemblies under references see below highlighted 1.  System.Configuration.Install  2.  System.ServiceProcess. 

Create Setup Project

Build your service project, you need to Setup project to install the build/compiled project and run the installers to run the Windows service. Create a new project as a “Setup Project”  and need to add project output :-
Create Setup Project for Window Service
Go to solution explorer & right click on solution, go to Add > New Project…

Open a dialog box, go to left pane under template > Other Project Type > Setup and Deployment > Visual Studio Installer and go to  right pane select project as a “Setup Project” and click on OK button.

Custom actions add to setup project. 
Go to solution, right click on setup project > View > Custom action.

Appears “Custom action” editor.
Right click on “Custom action > Add Custom action… ”

Open a dialog box “Select Item In Project” : Double click on “Application Folder” after that click on “Add Output…” button. Open a dialog box, choose your project (Window service) and “Primary Output” from (Active) and click on OK button.

   Build your setup project and install the window service

Install And Start The Service

Go to solution explorer > Build both project (Window Service & Setup Project) > Right Click on Setup Project and Click on Install Option > and follow setup wizard steps. Finally your service ready for start & stop. If you window server than go to – Start > Administrator tools > Service > Find Your Service > Right Click on Service Name > Start.  Otherwise – Start > Computer (My Computer) > Manage  > Click on Service And Application from left pane > Services.  


Post a Comment