0

I have wix project. With Installation all is ok. But when I do the same (passing parameter during unistall) its not working((

Wix file

<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>

My Custom Action

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;
            }

        }
    }

In BeforeUnistall method session.CustomActionData is empty when unistalling. But in AfterIntall method all parameters exists. What is my problem? What is wrong?

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
RomanReaL D
  • 101
  • 5
  • [Maybe have a quick read here](https://stackoverflow.com/a/54728485/129130). Throwing in a link to an answer on [deferred mode property access](https://stackoverflow.com/a/54298965/129130) as well. – Stein Åsmul Mar 13 '19 at 00:18

2 Answers2

0

I resolved my problem. I store path in registry and get it on uninstall.

RomanReaL D
  • 101
  • 5
0

Windows Installer doesn't persist properties after an installation completes. You need to do it yourself such as this example.

http://robmensching.com/blog/posts/2010/5/2/the-wix-toolsets-remember-property-pattern/

IsWiX generated projects include boilerplate code as an example here.

(Lines 41-61)

https://github.com/iswix-llc/iswix/blob/master/Application/IsWiXNewAddIn/MSISolutionTemplate/SetupProjectTemplate/UI-CustomDialog.wxs

Christopher Painter
  • 54,556
  • 6
  • 63
  • 100