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:
- Switch to the design view for the service.
- Right click on it and select Add Installer. (This will add the
ProjectInstaller
file.) - Switch to the design view of the
ProjectInstaller
that is added. - Set the properties of the
serviceInstaller1
component.
ServiceName = TestService
StartType = Manual
- Set the properties of the
serviceProcessInstaller1
component.
- 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();
}
- 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.
- Right click on Setup project >> Add >> Project output.
- 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.
- Now open the custom action window by right clicking on window service project >> View >>
Custom Actions
- 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)
- 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
- 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]"
- 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
- Build the project.
This setup will fulfill the requirement mentioned in the Introduction section.