Search code examples
c#deploymentwindows-installer

Deployment: this.Context.Parameters not available throughout installation process


I have the following code but although I can access a property and retrieve a value

 this.Context.Parameters["SERVICENAME"] 

in BeforeInstall, the same property returns "" in OnCommitted.

Where does this data go, how does it gets wiped, where can I find a breakdown of the order of each of these methods and what gets passed where?

[RunInstaller(true)]
    public partial class ProjectInstaller : System.Configuration.Install.Installer
    {

        public string ServiceName { get; protected set; }

        /// <summary>
        /// 
        /// </summary>
        public ProjectInstaller()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="savedState"></param>
        protected override void OnBeforeInstall(IDictionary savedState)
        {
            base.OnBeforeInstall(savedState);
            this.ServiceName = this.Context.Parameters["SERVICENAME"].ToString();
            this.serviceInstaller1.ServiceName = this.ServiceName;
            this.serviceInstaller1.DisplayName = this.ServiceName;
        }

        /// <summary>
        /// /
        /// </summary>
        /// <param name="savedState"></param>
        protected override void OnCommitted(IDictionary savedState)
        {
            base.OnCommitted(savedState);
            string targetDirectory = Path.GetDirectoryName(Context.Parameters["AssemblyPath"]); ;
            string path = System.IO.Path.Combine(targetDirectory, "Services.Win32.exe.config");
            System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
            xDoc.Load(path);
            System.Xml.XmlNode node = xDoc.SelectSingleNode("/configuration/applicationSettings/Services.Win32.Properties.Settings/setting[@name='TaskManagerServiceName']/value");
            node.InnerText = (this.ServiceName); // here this.ServiceName is "" so was this.Context.Parameters[""SERVICENAME"] when i was using that
            xDoc.Save(path);
        }

Solution

  • The easiest, cleanest and most robust solution is to not use an installer class custom action to install a service. Use Windows Installer's built in mechansim: the ServiceInstall table.

    The problem is you are likely using Visual Studio Deployment Projects which don't expose this capability. No problem. Use Windows Installer XML to author a Merge Module that encapsulates your XE/Service component. Then add this merge module to your VDPROJ installer.

    See the following articles for ideas on how to wire this up:

    Augmenting InstallShield using Windows Installer XML - Certificates

    Augmenting InstallShield using Windows Installer XML - Windows Services

    Redemption of Visual Studio Deployment Projects