Click here to Skip to main content
Click here to Skip to main content
Go to top

Window Service Deployment using VS 2010

, 22 Oct 2013
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

Comments and Discussions

 
QuestionFiles in Use Dialog PinmemberSaransh Chintha24-Jun-14 17:12 
Questionupdates for vs 2012 - vs 2013 Pinmemberkiquenet.com9-Jun-14 1:44 
QuestionUpgrade Installed successfully but New upgraded service instance not running. PinmemberMember 1029682021-Oct-13 19:44 
AnswerRe: Upgrade Installed successfully but New upgraded service instance not running. PinprofessionalTechnoGeek00121-Oct-13 20:31 
GeneralRe: Upgrade Installed successfully but New upgraded service instance not running. PinmemberMember 1029682021-Oct-13 20:41 
GeneralRe: Upgrade Installed successfully but New upgraded service instance not running. PinprofessionalTechnoGeek00121-Oct-13 21:24 
GeneralRe: Upgrade Installed successfully but New upgraded service instance not running. PinmemberMember 1029682021-Oct-13 22:30 
QuestionIt helped with my task Pinmemberfretum11-Jul-13 21:52 
GeneralMy vote of 4 PinmemberNiranjan N Tantri26-Apr-13 2:12 
GeneralRe: My vote of 4 PinmemberTechnoGeek00126-Apr-13 5:24 
QuestionUpgrading Windows service to .net4.0 from .net2.0 PinmemberSatish Bab26-Apr-13 0:01 
AnswerRe: Upgrading Windows service to .net4.0 from .net2.0 PinmemberTechnoGeek00126-Apr-13 5:22 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0 PinmemberSatish Bab3-May-13 1:31 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0 PinprofessionalTechnoGeek0013-May-13 20:43 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0 PinmemberSatish Bab7-May-13 4:06 
QuestionException Handling/Rethrowing PinmemberMember 379640325-Apr-13 19:20 
AnswerRe: Exception Handling/Rethrowing PinmemberTechnoGeek00126-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 | Mobile
Web01 | 2.8.140916.1 | Last Updated 22 Oct 2013
Article Copyright 2013 by tgeek001
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid