简体   繁体   English

使用MSI安装程序升级服务

[英]upgrade a service using MSI installer

I am upgrading my custom windows service using MSI installer. 我正在使用MSI安装程序升级自定义Windows服务。 I am using C# code to start MSI process to first uninstall the service and then install new version. 我正在使用C#代码启动MSI进程,首先卸载该服务,然后安装新版本。

I need to make sure that before MSI starts installing new version ,previous version is uninstalled . 我需要确保在MSI开始安装新版本之前,已卸载旧版本 How do I add this check in c#? 如何在C#中添加此检查?

I recommend using WIX to do this. 我建议使用WIX执行此操作。 A wrapper around MSI http://wixtoolset.org/documentation/ MSI的包装器http://wixtoolset.org/documentation/

Make sure to increment the version as a best practice. 确保增加版本,这是最佳做法。

Key is to increment the Version attribute or set AllowSameVersionUpgrades="yes" and not change UpgradeCode="[your unique upgradecode here]". 关键是增加Version属性设置AllowSameVersionUpgrades =“ yes”,而不更改UpgradeCode =“ [此处为您唯一的升级代码]”。 Make sure that the UpgradeCode attribute remains the same it has to be static so should not be set to * which will generate a random GUID. 确保UpgradeCode属性保持不变,它必须是静态的,因此不应将其设置为*,这会生成随机的GUID。

<MajorUpgrade AllowSameVersionUpgrades="yes" DowngradeErrorMessage="A newer version of [ProductName] is already installed." />

 <Product
Name="Sample"
Id="*"
UpgradeCode="4c79fec3-a6b7-46eb-90d6-46688a7f1662"
Manufacturer="Sample"
Version="0.1.3.0"
Language="1033"> ... />

Your question is how to do this in C# 您的问题是如何在C#中执行此操作

check the registry key HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\ 检查注册表项HKLM:\\ Software \\ Microsoft \\ Windows \\ CurrentVersion \\ Uninstall \\

SC QUERY you probably just want to replace that part with C# code. SC QUERY您可能只想用C#代码替换该部分。 I strongly advise against using most of the built-in Windows command-line programs. 我强烈建议您不要使用大多数内置的Windows命令行程序。

I am using InstallSheild .. This issue was happening because by the time MSI installer could remove the SERVICE from registry, my code starts installing the new version of it. 我正在使用InstallSheild ..之所以发生此问题,是因为当MSI安装程序可以从注册表中删除SERVICE时,我的代码开始安装它的新版本。

So the MSI does remove the service from machine registry but because of the time lag in this process, new version installation fails. 因此,MSI确实从计算机注册表中删除了该服务,但是由于此过程中存在时间滞后,因此新版本安装失败。 This happens in server machines mostly. 这主要发生在服务器计算机中。 Don't know the reason for it. 不知道原因。

To fix it, i am now checking service existence before installation using sc query servicename and so while service exists I am doing thread.sleep(1000) 为了解决这个问题,我现在正在使用sc query servicename在安装之前检查服务是否存在,因此当服务存在时我正在执行thread.sleep(1000)

Please share a better approach if any. 如果有的话,请分享更好的方法。 But I have to do it using InstallSheild only. 但是我只能使用InstallSheild来做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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