windows “由于缺少 ServiceProcessInstaller 导致安装失败”问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2765313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:24:31  来源:igfitidea点击:

"Installation failed due to the absence of a ServiceProcessInstaller" Problem

windowsc#-3.0installationservicevisual-studio-2008-sp1

提问by Furqan Misarwala

When I start to installing using installutil it gives me following error, I have set ServiceInstaller and ServiceInstallerProcess,

当我开始使用 installutil 安装时,它给了我以下错误,我已经设置了 ServiceInstaller 和 ServiceInstallerProcess,

System.InvalidOperationException: Installation failed due to the absence of a ServiceProcessInstaller. The ServiceProcessInstaller must either be the containing installer, or it must be present in the Installers collection on the same installer as the ServiceInstaller.

System.InvalidOperationException:由于缺少 ServiceProcessInstaller,安装失败。ServiceProcessInstaller 必须是包含的安装程序,或者它必须存在于与 ServiceInstaller 相同的安装程序上的 Installers 集合中。

Any ideas on how to fix the problem?

关于如何解决问题的任何想法?

回答by jmozko

I had the same problem with the Installer and found that in the [YourInstallerClassName].Designer.cs at InitializeComponent() method, the dfault generated code is Missing add the ServiceProcessInstaller

我在安装程序中遇到了同样的问题,发现在 InitializeComponent() 方法的 [YourInstallerClassName].Designer.cs 中,默认生成的代码是 Missing add the ServiceProcessInstaller

        // 
        // [YourInstallerClassName]
        // 
        this.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.serviceInstaller1});

Just add your ServiceProcessInstaller in my case its:

只需在我的情况下添加您的 ServiceProcessInstaller:

        // 
        // ProjectInstaller
        // 
        this.Installers.AddRange(new System.Configuration.Install.Installer[] {
        this.serviceProcessInstaller1,   //--> Missing
        this.serviceInstaller1});

and the Setup project works.

并且安装项目有效。

回答by Tom Cabanski

Usually, this means you failed to attribute your installer with RunInstaller(true). Here's an example of one I have handy that works:

通常,这意味着您未能将安装程序归因于 RunInstaller(true)。这是我手头的一个例子:

namespace OnpointConnect.WindowsService
{
    [RunInstaller(true)]
    public partial class OnpointConnectServiceInstaller : Installer
    {
        private ServiceProcessInstaller processInstaller;
        private ServiceInstaller serviceInstaller;

        public OnpointConnectServiceInstaller()
        {
            InitializeComponent();
        }

        public override string HelpText
        {
            get
            {
                return
                    "/name=[service name]\nThe name to give the OnpointConnect Service.  " +
                    "The default is OnpointConnect.  Note that each instance of the service should be installed from a unique directory with its own config file and database.";
            }
        }

        public override void Install(IDictionary stateSaver)
        {
            Initialize();
            base.Install(stateSaver);
        }

        public override void Uninstall(IDictionary stateSaver)
        {
            Initialize();
            base.Uninstall(stateSaver);
        }

        private void Initialize()
        {
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller = new ServiceInstaller();
            processInstaller.Account = ServiceAccount.LocalSystem;
            serviceInstaller.StartType = ServiceStartMode.Manual;

            string serviceName = "OnpointConnect";
            if (Context.Parameters["name"] != null)
            {
                serviceName = Context.Parameters["name"];
            }
            Context.LogMessage("The service name = " + serviceName);

            serviceInstaller.ServiceName = serviceName;

            try
            {
                //stash the service name in a file for later use in the service
                var writer = new StreamWriter("ServiceName.dat");
                try
                {
                    writer.WriteLine(serviceName);
                }
                finally
                {
                    writer.Close();
                }

                Installers.Add(serviceInstaller);
                Installers.Add(processInstaller);
            }
            catch (Exception err)
            {
                Context.LogMessage("An error occured while creating configuration information for the service.  The error is "
                                   + err.Message);
            }
        }
    }
}