简体   繁体   中英

how do i create the Setup windows service to uninstall then install service?

I developed a windows service in C#. and my project has Installer, and Setup project.
the setup project, installs service very well and it doesn't have any problem, but when I change project and create other setup, the new setup doesn't install new service, because it's already exist!!! is any way to create setup that it uninstalls the service then install it?

The problem is that upgrades in VS2008 setups and later use installer classes and an install sequence which is 1) Install newer product over older product, use file version checking, run install custom actions 2) Remove the old product running uninstall custom actions and getting rid of files that are no longer used. VS 2005 wasn't like this. So the installer class custom action that installs the service is running when the service already exists. Sometimes you can revert to the VS 2005 upgrade mechanism that removes ALL the older installed files first, including databases etc, so keep that in mind. You can do that by modifying the MSI file with Orca, InstallExecuteSequence table, and re-sequence the RemoveExistingProducts action to immediately after InstallValidate. Otherwise do both these:

  1. Always install the service binary to a fixed location (that means not the browsable Program Files folder). The uninstall custom action for the service should have a condition of NOT UPGRADINGPRODUCTCODE so that an upgrade doesn't try to uninstall it (but a straight uninstall will). It needs to be in a fixed location because this upgrade solution doesn't install or uninstall the service - it just updates the binary - but obviously you don't want service entries referring to a service that has changed location.

  2. In the upgrade have a condition of NOT PREVIOUSVERSIONSINSTALLED on the install custom action for the service. This means it will not try to re-install the service (which is why you get the "already exists" error) but it will update the exe if you have incremented its file version.

If you search the MSDN setup forum http://social.msdn.microsoft.com/Forums/en-US/home?forum=winformssetup for PREVIOUSVERSIONSINSTALLED many of the posts are to do with this issue and there is more discussion and explanation.

Editorial: Installer classes are not required because Windows Installer can do all this for you, which is why this is only ever an issue with VS setup projects. Moving to a tool that uses MSIs ServiceInstall and ServiceControl tables will get you out of the need for any code to install/uninstall/stop/start services.

You could do it by properly configuring MSI properties. Click on Installer project and press F4 to view MSI's properties window.

  1. When you go to properties you could see two type of properties that are Upgrade code and Product Code . Every Time you create/release a new Windows service installer make sure to keep the same Upgrade code but change the Product Code.

  2. Change DetectNewerInstalledVersions property to true .

  3. Set RemovePreviousVersions to true .

  4. Now Change the version number to a higher version from previous release, if your previous release version was 1.0.2, change the new version to 1.0.3.

When you install a MSI with abovementioned settings, MSI will check if any other product installed with same Upgrade code, if it finds a product then it will check if the new installation has a higher version. If all the conditions are satisfied it will first remove the existing(Older) version and install the newer version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM