在卸载期间将参数传递给自定义操作
Passing parameters into custom action during unistall
我有wix项目。安装一切正常。但是当我做同样的事情时(在卸载期间传递参数)它不起作用((
Wix 文件
<Product Id="$(var.ProductCode)" Name="$(var.ProductName)" Language="1033" Version="$(var.ProductVersion)" Manufacturer="$(var.Manufacturer)" UpgradeCode="$(var.UpgradeCode)">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine"/>
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<MediaTemplate EmbedCab="yes" />
<Feature Id="ProductFeature" Title="VMP.Passport.Installers.Server" Level="1">
<ComponentGroupRef Id="ProductComponents" />
<ComponentGroupRef Id="ServerInstallerFiles" />
<ComponentGroupRef Id="DataInitInstallerFiles" />
</Feature>
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" ></Property>
<UIRef Id="WixUI_MinimalCustom"/>
<InstallExecuteSequence>
<Custom Action="DoAfterInstallJobParams" Before="DoAfterInstallJob">Not Installed</Custom>
<Custom Action="DoAfterInstallJob" After="InstallFiles">Not Installed</Custom>
<Custom Action="DoBeforeUnstallJobParams" Before="DoBeforeUnstallJob">REMOVE="ALL"</Custom>
<Custom Action="DoBeforeUnstallJob" After="InstallInitialize">REMOVE="ALL"</Custom>
</InstallExecuteSequence>
</Product>
<Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLFOLDER" Name="VMP" />
</Directory>
</Directory>
</Fragment>
<Fragment>
<Property Id="DoBeforeUninstallJob" Value="[INSTALLFOLDER]" />
<Binary Id="CustomActionBinary" SourceFile="$(var.SolutionDir)Output\Installers\Actions\VMP.Passport.Installers.Server.Actions.CA.dll" />
<CustomAction Id="DoAfterInstallJob" BinaryKey="CustomActionBinary" DllEntry="AfterIntall" Execute="deferred" Return="check" Impersonate="no" />
<CustomAction Id="DoAfterInstallJobParams" Property="DoAfterInstallJob" Value="HOSTING_URL=[HOSTING_URL];HOSTING_PORT=[HOSTING_PORT];DB_CONNECTION=[DB_CONNECTION];INSTALLPATH=[INSTALLFOLDER]" />
<CustomAction Id="DoBeforeUnstallJob" BinaryKey="CustomActionBinary" DllEntry="BeforeUnistall" Execute="deferred" Return="check" Impersonate="no" />
<CustomAction Id="DoBeforeUnstallJobParams" Property="DoBeforeUninstallJob" Value="INSTALLPATH=[INSTALLFOLDER]" />
</Fragment>
<Fragment>
<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
<ComponentRef Id="cmpDataInit"/>
<ComponentRef Id="cmpServerHost"/>
</ComponentGroup>
</Fragment>
<Fragment>
<DirectoryRef Id="INSTALLFOLDER">
<Directory Id="DataInit" Name="DataInit">
<Component Id="cmpDataInit">
<File Id="DataInitLog4netConfig" Source="$(var.SolutionDir)..\Logging\log4net.config" />
</Component>
</Directory>
</DirectoryRef>
<DirectoryRef Id="INSTALLFOLDER">
<Directory Id="ServerHost" Name="ServerHost">
<Component Id="cmpServerHost">
<File Id="ServerLog4netConfig" Source="$(var.SolutionDir)..\Logging\log4net.config" />
</Component>
</Directory>
</DirectoryRef>
</Fragment>
我的自定义操作
public class CustomActions
{
protected static string sourceLogFilesPath = "\"Logs\";
protected static string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
[CustomAction]
public static ActionResult AfterIntall(Session session)
{
try
{
string rootPath = session.CustomActionData["INSTALLPATH"];
string installPath = Path.Combine(session.CustomActionData["INSTALLPATH"], "ServerHost");
#region Coping Log4net config file and change config path
string passportConfigFile = Path.Combine(installPath, "VMP.Core.ServerHost.exe.config");
string sourceLogPath = @"loggerConfigFile=""..\..\..\Logging\log4net.config""";
string destLogPath = @"loggerConfigFile=""log4net.config""";
//passport server
string passportConfigText = File.ReadAllText(passportConfigFile);
passportConfigText = passportConfigText.Replace(sourceLogPath, destLogPath);
File.WriteAllText(passportConfigFile, passportConfigText);
//datainit
string dataInitConfigFile = Path.Combine(rootPath, "DataInit", "VMP.Passport.DataInit.exe.config");
string dataConfigText = File.ReadAllText(dataInitConfigFile);
dataConfigText = dataConfigText.Replace(sourceLogPath, destLogPath);
File.WriteAllText(dataInitConfigFile, dataConfigText);
#endregion
#region Changing passport server app.config
string passportServerExeFile = Path.Combine(installPath, "VMP.Core.ServerHost.exe");
var configFile = ConfigurationManager.OpenExeConfiguration(passportServerExeFile);
//Changing hosting params
configFile.AppSettings.Settings["serverUrl"].Value = session.CustomActionData["HOSTING_URL"];
configFile.AppSettings.Settings["startingPort"].Value = session.CustomActionData["HOSTING_PORT"];
//Changing db server name
string dbconnection = session.CustomActionData["DB_CONNECTION"];
var connectionStringsSection = (ConnectionStringsSection)configFile.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["RootDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.RootDB;Integrated Security=True";
connectionStringsSection.ConnectionStrings["PassportDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.PassportDB;Integrated Security=True";
connectionStringsSection.ConnectionStrings["ContentDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ContentDB;Integrated Security=True";
connectionStringsSection.ConnectionStrings["ShamirDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ShamirDB;Integrated Security=True";
connectionStringsSection.ConnectionStrings["ReplicationDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ReplicationDB;Integrated Security=True";
configFile.Save();
string dataInitConfigurationFileName = Path.Combine(rootPath, "DataInit", "VMP.Passport.DataInit.exe");
var dataInitConfigurationFile = ConfigurationManager.OpenExeConfiguration(dataInitConfigurationFileName);
var dataInitconnectionStringsSection = (ConnectionStringsSection)dataInitConfigurationFile.GetSection("connectionStrings");
dataInitconnectionStringsSection.ConnectionStrings["RootDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.RootDB;Integrated Security=True";
dataInitconnectionStringsSection.ConnectionStrings["PassportDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.PassportDB;Integrated Security=True";
dataInitconnectionStringsSection.ConnectionStrings["ContentDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ContentDB;Integrated Security=True";
dataInitconnectionStringsSection.ConnectionStrings["ReplicationDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ReplicationDB;Integrated Security=True";
dataInitConfigurationFile.Save();
#endregion
#region Start dataInit
string dataInitExeFile = Path.Combine(rootPath, "DataInit", "VMP.Passport.DataInit.exe");
Process.Start(dataInitExeFile);
#endregion
#region Installing win service
string batFile = Path.Combine(installPath, "InstallService.bat");
string command = $@"
{installPath.Substring(0, 2)}
cd {installPath}
VMP.Core.ServerHost.exe install
VMP.Core.ServerHost.exe start";
File.WriteAllText(batFile, command);
string batUnistallFile = Path.Combine(installPath, "UninstallService.bat");
string commandUnistall = $@"
{installPath.Substring(0, 2)}
cd {installPath}
VMP.Core.ServerHost.exe uninstall
pause";
File.WriteAllText(batUnistallFile, commandUnistall);
//Process.Start(batFile);
#endregion
return ActionResult.Success;
}
catch (Exception exc)
{
File.AppendAllText(Path.Combine(documentsPath, "install_log.txt"), exc.ToString());
return ActionResult.Failure;
}
}
[CustomAction]
public static ActionResult BeforeUnistall(Session session)
{
try
{
string installPath = Path.Combine(session.CustomActionData["INSTALLPATH"], "ServerHost");
string batUnistallFile = Path.Combine(installPath, "UninstallService.bat");
string commandUnistall = $@"
{installPath.Substring(0, 2)}
cd {installPath}
VMP.Core.ServerHost.exe uninstall";
File.WriteAllText(batUnistallFile, commandUnistall);
Process.Start(batUnistallFile).WaitForExit(10000);
return ActionResult.Success;
}
catch (Exception exc)
{
File.AppendAllText(Path.Combine(documentsPath, "install_log.txt"), exc.ToString());
return ActionResult.Failure;
}
}
}
在 BeforeUnistall 方法中 session.CustomActionData 卸载时为空。但是在 AfterIntall 方法中所有参数都存在。
我的问题是什么?怎么了?
我解决了我的问题。我将路径存储在注册表中并在卸载时获取它。
Windows 安装程序在安装完成后不会保留属性。你需要自己做,比如这个例子。
http://robmensching.com/blog/posts/2010/5/2/the-wix-toolsets-remember-property-pattern/
IsWiX 生成的项目包括样板代码作为示例。
(第 41-61 行)
我有wix项目。安装一切正常。但是当我做同样的事情时(在卸载期间传递参数)它不起作用((
Wix 文件
<Product Id="$(var.ProductCode)" Name="$(var.ProductName)" Language="1033" Version="$(var.ProductVersion)" Manufacturer="$(var.Manufacturer)" UpgradeCode="$(var.UpgradeCode)">
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine"/>
<MajorUpgrade DowngradeErrorMessage="A newer version of [ProductName] is already installed." />
<MediaTemplate EmbedCab="yes" />
<Feature Id="ProductFeature" Title="VMP.Passport.Installers.Server" Level="1">
<ComponentGroupRef Id="ProductComponents" />
<ComponentGroupRef Id="ServerInstallerFiles" />
<ComponentGroupRef Id="DataInitInstallerFiles" />
</Feature>
<Property Id="WIXUI_INSTALLDIR" Value="INSTALLFOLDER" ></Property>
<UIRef Id="WixUI_MinimalCustom"/>
<InstallExecuteSequence>
<Custom Action="DoAfterInstallJobParams" Before="DoAfterInstallJob">Not Installed</Custom>
<Custom Action="DoAfterInstallJob" After="InstallFiles">Not Installed</Custom>
<Custom Action="DoBeforeUnstallJobParams" Before="DoBeforeUnstallJob">REMOVE="ALL"</Custom>
<Custom Action="DoBeforeUnstallJob" After="InstallInitialize">REMOVE="ALL"</Custom>
</InstallExecuteSequence>
</Product>
<Fragment>
<Directory Id="TARGETDIR" Name="SourceDir">
<Directory Id="ProgramFilesFolder">
<Directory Id="INSTALLFOLDER" Name="VMP" />
</Directory>
</Directory>
</Fragment>
<Fragment>
<Property Id="DoBeforeUninstallJob" Value="[INSTALLFOLDER]" />
<Binary Id="CustomActionBinary" SourceFile="$(var.SolutionDir)Output\Installers\Actions\VMP.Passport.Installers.Server.Actions.CA.dll" />
<CustomAction Id="DoAfterInstallJob" BinaryKey="CustomActionBinary" DllEntry="AfterIntall" Execute="deferred" Return="check" Impersonate="no" />
<CustomAction Id="DoAfterInstallJobParams" Property="DoAfterInstallJob" Value="HOSTING_URL=[HOSTING_URL];HOSTING_PORT=[HOSTING_PORT];DB_CONNECTION=[DB_CONNECTION];INSTALLPATH=[INSTALLFOLDER]" />
<CustomAction Id="DoBeforeUnstallJob" BinaryKey="CustomActionBinary" DllEntry="BeforeUnistall" Execute="deferred" Return="check" Impersonate="no" />
<CustomAction Id="DoBeforeUnstallJobParams" Property="DoBeforeUninstallJob" Value="INSTALLPATH=[INSTALLFOLDER]" />
</Fragment>
<Fragment>
<ComponentGroup Id="ProductComponents" Directory="INSTALLFOLDER">
<ComponentRef Id="cmpDataInit"/>
<ComponentRef Id="cmpServerHost"/>
</ComponentGroup>
</Fragment>
<Fragment>
<DirectoryRef Id="INSTALLFOLDER">
<Directory Id="DataInit" Name="DataInit">
<Component Id="cmpDataInit">
<File Id="DataInitLog4netConfig" Source="$(var.SolutionDir)..\Logging\log4net.config" />
</Component>
</Directory>
</DirectoryRef>
<DirectoryRef Id="INSTALLFOLDER">
<Directory Id="ServerHost" Name="ServerHost">
<Component Id="cmpServerHost">
<File Id="ServerLog4netConfig" Source="$(var.SolutionDir)..\Logging\log4net.config" />
</Component>
</Directory>
</DirectoryRef>
</Fragment>
我的自定义操作
public class CustomActions
{
protected static string sourceLogFilesPath = "\"Logs\";
protected static string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments);
[CustomAction]
public static ActionResult AfterIntall(Session session)
{
try
{
string rootPath = session.CustomActionData["INSTALLPATH"];
string installPath = Path.Combine(session.CustomActionData["INSTALLPATH"], "ServerHost");
#region Coping Log4net config file and change config path
string passportConfigFile = Path.Combine(installPath, "VMP.Core.ServerHost.exe.config");
string sourceLogPath = @"loggerConfigFile=""..\..\..\Logging\log4net.config""";
string destLogPath = @"loggerConfigFile=""log4net.config""";
//passport server
string passportConfigText = File.ReadAllText(passportConfigFile);
passportConfigText = passportConfigText.Replace(sourceLogPath, destLogPath);
File.WriteAllText(passportConfigFile, passportConfigText);
//datainit
string dataInitConfigFile = Path.Combine(rootPath, "DataInit", "VMP.Passport.DataInit.exe.config");
string dataConfigText = File.ReadAllText(dataInitConfigFile);
dataConfigText = dataConfigText.Replace(sourceLogPath, destLogPath);
File.WriteAllText(dataInitConfigFile, dataConfigText);
#endregion
#region Changing passport server app.config
string passportServerExeFile = Path.Combine(installPath, "VMP.Core.ServerHost.exe");
var configFile = ConfigurationManager.OpenExeConfiguration(passportServerExeFile);
//Changing hosting params
configFile.AppSettings.Settings["serverUrl"].Value = session.CustomActionData["HOSTING_URL"];
configFile.AppSettings.Settings["startingPort"].Value = session.CustomActionData["HOSTING_PORT"];
//Changing db server name
string dbconnection = session.CustomActionData["DB_CONNECTION"];
var connectionStringsSection = (ConnectionStringsSection)configFile.GetSection("connectionStrings");
connectionStringsSection.ConnectionStrings["RootDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.RootDB;Integrated Security=True";
connectionStringsSection.ConnectionStrings["PassportDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.PassportDB;Integrated Security=True";
connectionStringsSection.ConnectionStrings["ContentDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ContentDB;Integrated Security=True";
connectionStringsSection.ConnectionStrings["ShamirDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ShamirDB;Integrated Security=True";
connectionStringsSection.ConnectionStrings["ReplicationDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ReplicationDB;Integrated Security=True";
configFile.Save();
string dataInitConfigurationFileName = Path.Combine(rootPath, "DataInit", "VMP.Passport.DataInit.exe");
var dataInitConfigurationFile = ConfigurationManager.OpenExeConfiguration(dataInitConfigurationFileName);
var dataInitconnectionStringsSection = (ConnectionStringsSection)dataInitConfigurationFile.GetSection("connectionStrings");
dataInitconnectionStringsSection.ConnectionStrings["RootDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.RootDB;Integrated Security=True";
dataInitconnectionStringsSection.ConnectionStrings["PassportDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.PassportDB;Integrated Security=True";
dataInitconnectionStringsSection.ConnectionStrings["ContentDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ContentDB;Integrated Security=True";
dataInitconnectionStringsSection.ConnectionStrings["ReplicationDB"].ConnectionString = $"Data Source={dbconnection};Initial Catalog=VMP.ReplicationDB;Integrated Security=True";
dataInitConfigurationFile.Save();
#endregion
#region Start dataInit
string dataInitExeFile = Path.Combine(rootPath, "DataInit", "VMP.Passport.DataInit.exe");
Process.Start(dataInitExeFile);
#endregion
#region Installing win service
string batFile = Path.Combine(installPath, "InstallService.bat");
string command = $@"
{installPath.Substring(0, 2)}
cd {installPath}
VMP.Core.ServerHost.exe install
VMP.Core.ServerHost.exe start";
File.WriteAllText(batFile, command);
string batUnistallFile = Path.Combine(installPath, "UninstallService.bat");
string commandUnistall = $@"
{installPath.Substring(0, 2)}
cd {installPath}
VMP.Core.ServerHost.exe uninstall
pause";
File.WriteAllText(batUnistallFile, commandUnistall);
//Process.Start(batFile);
#endregion
return ActionResult.Success;
}
catch (Exception exc)
{
File.AppendAllText(Path.Combine(documentsPath, "install_log.txt"), exc.ToString());
return ActionResult.Failure;
}
}
[CustomAction]
public static ActionResult BeforeUnistall(Session session)
{
try
{
string installPath = Path.Combine(session.CustomActionData["INSTALLPATH"], "ServerHost");
string batUnistallFile = Path.Combine(installPath, "UninstallService.bat");
string commandUnistall = $@"
{installPath.Substring(0, 2)}
cd {installPath}
VMP.Core.ServerHost.exe uninstall";
File.WriteAllText(batUnistallFile, commandUnistall);
Process.Start(batUnistallFile).WaitForExit(10000);
return ActionResult.Success;
}
catch (Exception exc)
{
File.AppendAllText(Path.Combine(documentsPath, "install_log.txt"), exc.ToString());
return ActionResult.Failure;
}
}
}
在 BeforeUnistall 方法中 session.CustomActionData 卸载时为空。但是在 AfterIntall 方法中所有参数都存在。 我的问题是什么?怎么了?
我解决了我的问题。我将路径存储在注册表中并在卸载时获取它。
Windows 安装程序在安装完成后不会保留属性。你需要自己做,比如这个例子。
http://robmensching.com/blog/posts/2010/5/2/the-wix-toolsets-remember-property-pattern/
IsWiX 生成的项目包括样板代码作为示例。
(第 41-61 行)