Click here to Skip to main content
11,712,454 members (73,572 online)
Click here to Skip to main content

Window Service Deployment using VS 2010

, 22 Oct 2013 CPOL 38.6K 836 30
Rate this:
Please Sign up or sign in to vote.
This tip explains how to deploy the window service using Visual Studio 2010.

Introduction

The purpose of this tip is to give a basic idea of how to deploy a Windows service using the Visual Studio deployment project. This tip covers almost all the things which you may require at the time of deployment of Windows Service. Following are some key points which are covered in this tip:

  • Deployment of Windows service using Visual Studio deployment project
  • Detection of previously installed windows service and preventing the error "Error 1001. The specified service already exists."
  • Start the Windows Service after installation of window service has been completed.

Using the Code

The below steps need to be followed to add Installer:

  1. Switch to the design view for the service.
  2. Right click on it and select Add Installer. (This will add the ProjectInstaller file.)
  3. Switch to the design view of the ProjectInstaller that is added.
  4. Set the properties of the serviceInstaller1 component.
    • ServiceName = TestService
    • StartType = Manual
  5. Set the properties of the serviceProcessInstaller1 component.
    • Account = LocalSystem
  6. Add the following events for the services to stop the service before install and start automatically after install.
    private void serviceInstaller1_BeforeInstall(object sender, InstallEventArgs e)
    {
        try
        {
            ServiceController controller = ServiceController.GetServices().Where
            (s => s.ServiceName == serviceInstaller1.ServiceName).FirstOrDefault();
            if (controller != null)
            {
                if ((controller.Status != ServiceControllerStatus.Stopped) && 
                (controller.Status != ServiceControllerStatus.StopPending))
                {
                    controller.Stop();
                }
            }
        }
        catch (Exception ex)
        {
            throw new System.Configuration.Install.InstallException
                (ex.Message.ToString());
        }
    } 
    
    private void serviceInstaller1_BeforeUninstall(object sender, InstallEventArgs e)
    {
        try
        {
            ServiceController controller = ServiceController.GetServices().Where
            (s => s.ServiceName == serviceInstaller1.ServiceName).FirstOrDefault();
            if (controller != null)
            {
                if ((controller.Status != ServiceControllerStatus.Stopped) && 
                (controller.Status != ServiceControllerStatus.StopPending))
                {
                    controller.Stop();
                }
            }
        }
        catch (Exception ex)
        {
            throw new System.Configuration.Install.InstallException
                (ex.Message.ToString());
        }
    } 
    
    private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
    {
        new ServiceController(serviceInstaller1.ServiceName).Start();
    } 
  7. Build the solution.

Now add the Setup project in the same solution and follow the steps given below to configure the setup project to deploy windows service automatically.

  1. Right click on Setup project >> Add >> Project output.
  2. This will add project output group screen will open and from here add the primary output of the window service with the selection of "Release" mode from the configuration dropdown.
  3. Now open the custom action window by right clicking on window service project >> View >> Custom Actions
  4. From this window, right click on install and add custom action and select the primary output from the application folder. Set the following line in the Condition property to detect if the Windows Service was previously installed or not.
    NOT (Installed or PREVIOUSVERSIONSINSTALLED) 
  5. Now right click on uninstall and add custom action and select the primary output from the application folder. Set the following line in the Condition property to upgrade setup not try to uninstall the Windows Service.
    NOT UPGRADINGPRODUCTCODE 
  6. Now right click on Commit and add custom action and select the primary output from the application folder. Set the CustomActionData property as given below to get the product code of the previously installed version.
    /OldProductCode="[PREVIOUSVERSIONSINSTALLED]"
  7. Now the important step is to set the following property from the Windows Service project prosperity box to call the uninstall process before install new setup.
    RemovePreviousVersions = True   
  8. Build the project.

This setup will fulfill the requirement mentioned in the Introduction section.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

tgeek001
Technical Lead
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionFiles in Use Dialog Pin
Saransh Chintha24-Jun-14 17:12
memberSaransh Chintha24-Jun-14 17:12 
AnswerRe: Files in Use Dialog Pin
Member 1151901512-Mar-15 23:12
memberMember 1151901512-Mar-15 23:12 
Questionupdates for vs 2012 - vs 2013 Pin
kiquenet.com9-Jun-14 1:44
memberkiquenet.com9-Jun-14 1:44 
QuestionUpgrade Installed successfully but New upgraded service instance not running. Pin
Member 1029682021-Oct-13 19:44
memberMember 1029682021-Oct-13 19:44 
AnswerRe: Upgrade Installed successfully but New upgraded service instance not running. Pin
TechnoGeek00121-Oct-13 20:31
professionalTechnoGeek00121-Oct-13 20:31 
GeneralRe: Upgrade Installed successfully but New upgraded service instance not running. Pin
Member 1029682021-Oct-13 20:41
memberMember 1029682021-Oct-13 20:41 
GeneralRe: Upgrade Installed successfully but New upgraded service instance not running. Pin
TechnoGeek00121-Oct-13 21:24
professionalTechnoGeek00121-Oct-13 21:24 
GeneralRe: Upgrade Installed successfully but New upgraded service instance not running. Pin
Member 1029682021-Oct-13 22:30
memberMember 1029682021-Oct-13 22:30 
QuestionIt helped with my task Pin
fretum11-Jul-13 21:52
memberfretum11-Jul-13 21:52 
GeneralMy vote of 4 Pin
Niranjan N Tantri26-Apr-13 2:12
memberNiranjan N Tantri26-Apr-13 2:12 
GeneralRe: My vote of 4 Pin
TechnoGeek00126-Apr-13 5:24
memberTechnoGeek00126-Apr-13 5:24 
QuestionUpgrading Windows service to .net4.0 from .net2.0 Pin
Satish Bab26-Apr-13 0:01
memberSatish Bab26-Apr-13 0:01 
AnswerRe: Upgrading Windows service to .net4.0 from .net2.0 Pin
TechnoGeek00126-Apr-13 5:22
memberTechnoGeek00126-Apr-13 5:22 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0 Pin
Satish Bab3-May-13 1:31
memberSatish Bab3-May-13 1:31 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0 Pin
TechnoGeek0013-May-13 20:43
professionalTechnoGeek0013-May-13 20:43 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0 Pin
Satish Bab7-May-13 4:06
memberSatish Bab7-May-13 4:06 
QuestionException Handling/Rethrowing Pin
Member 379640325-Apr-13 19:20
memberMember 379640325-Apr-13 19:20 
AnswerRe: Exception Handling/Rethrowing Pin
TechnoGeek00126-Apr-13 5:23
memberTechnoGeek00126-Apr-13 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150819.1 | Last Updated 22 Oct 2013
Article Copyright 2013 by tgeek001
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid