Click here to Skip to main content
Click here to Skip to main content

Window Service Deployment using VS 2010

By , 3 May 2013
 

Introduction

The purpose of this tip is to give a basic idea of how to deploy windows service using 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 windows service 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 window 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)

About the Author

TechnoGeek001
Team Leader
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 4memberNiranjan N Tantri26 Apr '13 - 2:12 
GeneralRe: My vote of 4memberTechnoGeek00126 Apr '13 - 5:24 
QuestionUpgrading Windows service to .net4.0 from .net2.0memberSatish Bab26 Apr '13 - 0:01 
AnswerRe: Upgrading Windows service to .net4.0 from .net2.0memberTechnoGeek00126 Apr '13 - 5:22 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0memberSatish Bab3 May '13 - 1:31 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0professionalTechnoGeek0013 May '13 - 20:43 
GeneralRe: Upgrading Windows service to .net4.0 from .net2.0memberSatish Bab7 May '13 - 4:06 
QuestionException Handling/RethrowingmemberMember 379640325 Apr '13 - 19:20 
AnswerRe: Exception Handling/RethrowingmemberTechnoGeek00126 Apr '13 - 5:23 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130513.1 | Last Updated 3 May 2013
Article Copyright 2013 by TechnoGeek001
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid