Click here to Skip to main content
15,881,600 members
Articles / Programming Languages / C#
Tip/Trick

Window Service Deployment using VS 2010

Rate me:
Please Sign up or sign in to vote.
4.96/5 (9 votes)
22 Oct 2013CPOL2 min read 81.6K   1.2K   30   18
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.
    C#
    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)


Written By
Technical Lead
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionFiles in Use Dialog Pin
Saransh Chintha24-Jun-14 17:12
Saransh Chintha24-Jun-14 17:12 
AnswerRe: Files in Use Dialog Pin
Member 1151901512-Mar-15 23:12
Member 1151901512-Mar-15 23:12 
Questionupdates for vs 2012 - vs 2013 Pin
kiquenet.com9-Jun-14 1:44
professionalkiquenet.com9-Jun-14 1:44 
QuestionUpgrade Installed successfully but New upgraded service instance not running. Pin
Member 1029682021-Oct-13 19:44
Member 1029682021-Oct-13 19:44 
AnswerRe: Upgrade Installed successfully but New upgraded service instance not running. Pin
npdev1321-Oct-13 20:31
npdev1321-Oct-13 20:31 
GeneralRe: Upgrade Installed successfully but New upgraded service instance not running. Pin
Member 1029682021-Oct-13 20:41
Member 1029682021-Oct-13 20:41 
GeneralRe: Upgrade Installed successfully but New upgraded service instance not running. Pin
npdev1321-Oct-13 21:24
npdev1321-Oct-13 21:24 
GeneralRe: Upgrade Installed successfully but New upgraded service instance not running. Pin
Member 1029682021-Oct-13 22:30
Member 1029682021-Oct-13 22:30 
QuestionIt helped with my task Pin
Fretum11-Jul-13 21:52
Fretum11-Jul-13 21:52 
GeneralMy vote of 4 Pin
Niranjan279026-Apr-13 2:12
Niranjan279026-Apr-13 2:12 
GeneralRe: My vote of 4 Pin
npdev1326-Apr-13 5:24
npdev1326-Apr-13 5:24 
QuestionUpgrading Windows service to .net4.0 from .net2.0 Pin
Satish Bab26-Apr-13 0:01
Satish Bab26-Apr-13 0:01 
AnswerRe: Upgrading Windows service to .net4.0 from .net2.0 Pin
npdev1326-Apr-13 5:22
npdev1326-Apr-13 5:22 
Please review the code that I uploaded in that see the setup project file properties.
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0 Pin
Satish Bab3-May-13 1:31
Satish Bab3-May-13 1:31 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0 Pin
npdev133-May-13 20:43
npdev133-May-13 20:43 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0 Pin
Satish Bab7-May-13 4:06
Satish Bab7-May-13 4:06 
QuestionException Handling/Rethrowing Pin
Member 379640325-Apr-13 19:20
Member 379640325-Apr-13 19:20 
AnswerRe: Exception Handling/Rethrowing Pin
npdev1326-Apr-13 5:23
npdev1326-Apr-13 5:23 

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

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