An exception occurred in the OnAfterInstall event

2019-08-12 01:42发布

问题:

NOTE: I have researched this issue for the past 2 days, I have seen very similar questions and answers but none have resolved my situation, therefore I am posing this question here after thorough research.

I created a windows service and setup project in VS2013. Everything was working fine and the installer was working fine. I had an issue with the service not starting on the deployment machine because it could not find the .dll file it needed. While working on that, another issue started happening. On installation I started getting this error: Error 1001: An exception occurred in the OnAfterInstall event handler of "servicename here", invalid value of parameter name.

I am passing file paths from the installer in to the Context to write to the app.Config file. The values are coming across fine [ I did debug this successfully]

My Installer Class:

[RunInstaller(true)]
    public partial class InstallHelper : Installer
    {
        private readonly ServiceProcessInstaller processInstaller;
        private readonly ServiceInstaller serviceInstaller;

        public InstallHelper()
        {
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new ServiceInstaller();

            processInstaller.Account = ServiceAccount.LocalSystem;
            serviceInstaller.StartType = ServiceStartMode.Automatic;
            serviceInstaller.ServiceName = "myServiceName";


            this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
        }

        public override void Install(IDictionary statesaver)
        {
            System.Diagnostics.Debugger.Launch();

            base.Install(statesaver);// <-- exception occurs here, statesaver count = 0: I'm not sure if it's supposed to be count = 0 but that seems to be suspicious to me.

        try
        {
            // Grab data from the textbox values
            // This is set to /PathValue=[WATCHPATH]... in CustomActionData property
        string watchPath = null;
        string outputPath = null;
        string connType = null;
        string timeOut = null;
        string serialPort = null;
        string destIP = null;

        string parameters = Context.Parameters["PathValue"];
        char[] c = new char[] { ' ', ',' };
        string[] components = parameters.Split(c, StringSplitOptions.None);

        watchPath = components[0];
        outputPath = components[1];
        connType = components[2];
        timeOut = components[3];
        serialPort = components[4];
        destIP = components[5];

        // Get path to the executable that is being installed 
        string assemblyPath = Context.Parameters["assemblypath"];
        string appConfigPath = assemblyPath + ".config";

        // Write the path to the app.config file
        XmlDocument doc = new XmlDocument();
        doc.Load(appConfigPath);

        XmlNode configuration = null;
        foreach (XmlNode node in doc.ChildNodes)
            if (node.Name == "configuration")
                configuration = node;

        if(configuration != null)
        {
            // Get the appSettings node
            XmlNode settingNode = null;
            foreach(XmlNode node in configuration.ChildNodes)
                if (node.Name == "appSettings")
                    settingNode = node;

            if(settingNode != null)
            {
                // Get the node with the attribute key=FilePath

                XmlNode watchNode = null;
                XmlNode outputNode = null;
                XmlNode connTypeNode = null;
                XmlNode timeOutNode = null;
                XmlNode serialPortNode = null;
                XmlNode destIPNode = null;

                foreach (XmlNode node in settingNode.ChildNodes)
                    if (node.Attributes["key"] != null)
                        if (node.Attributes["key"].Value == "WatchPath")
                            watchNode = node;
                        else if (node.Attributes["key"].Value == "OutPutFile")
                            outputNode = node;
                        else if (node.Attributes["key"].Value == "ConnType")
                            connTypeNode = node;
                        else if (node.Attributes["key"].Value == "TimeOut")
                            timeOutNode = node;
                        else if (node.Attributes["key"].Value == "SerialPort")
                            serialPortNode = node;
                        else if (node.Attributes["key"].Value == "DestIP")
                            destIPNode = node;

                if(watchNode != null && outputNode != null)
                {
                    XmlAttribute xmlwatchAtt = watchNode.Attributes["value"];
                    XmlAttribute xmloutputAtt = outputNode.Attributes["value"];
                    XmlAttribute xmlconntypeAtt = connTypeNode.Attributes["value"];
                    XmlAttribute xmltimeOutAtt = timeOutNode.Attributes["value"];
                    XmlAttribute xmlserialPortAtt = serialPortNode.Attributes["value"];
                    XmlAttribute xmldestIpAtt = destIPNode.Attributes["value"];

                    // Update the appConfig file
                    xmlwatchAtt.Value = watchPath; 
                    xmloutputAtt.Value = outputPath;
                    xmlconntypeAtt.Value = connType;
                    xmltimeOutAtt.Value = timeOut;
                    xmlserialPortAtt.Value = serialPort;
                    xmldestIpAtt.Value = destIP;

                    doc.Save(appConfigPath); // Save the file
                }
            }

        }
    }
    catch(Exception e)
    {
        string err = e.Message;
    }

  }

I am also setting the service to start automatically after installation here:

void ServiceInstaller_AfterInstall(Object sender, InstallEventArgs e)
        {
            using(System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(serviceInstaller.ServiceName))
            {
                sc.Start();
            }
        }

The exact error I get is:

Error 1001: An exception occurred in the OnAfterInstall event handler of WindowsService.InstallHelper. --> Invalid value of parameter name.

I did check all the values I am passing thru from the installer, and they all have valid data in them.

Any help or suggestions would help!