【问题标题】:Windows Service Fails To Start Through ManagedInstallerClass But Succeeds Through InstallUtilWindows 服务无法通过 ManagedInstallerClass 启动,但通过 InstallUtil 成功
【发布时间】:2014-11-08 19:39:41
【问题描述】:

我在 Visual Studio 2012 (c#) 中创建了一个安装后需要启动的 Windows 服务。我已经阅读了很多文章和 StackOverflow 问题,但没有一个可以正常工作。 在主要功能中,我有:

static void Main(string []args)
{
     ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
} 

我已经注册了服务的 AfterInstall 事件。

private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
    using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
    {
        sc.Start();
    }
}

我以管理员身份登录。当我运行 .exe 文件(以管理员身份)时,它会尝试安装服务(将其保持在启动状态 2 分钟)但无法启动它。当我在调试模式下运行时,我在 sc.Start() 上遇到异常。日志文件说:

 System.InvalidOperationException: An exception occurred in the OnAfterInstall event handler of System.ServiceProcess.ServiceInstaller.
 The inner exception System.InvalidOperationException was thrown with the following error message: Cannot start service Database Convertor on computer '.'.
The inner exception System.ComponentModel.Win32Exception was thrown with the following error message: The service did not respond to the start or control request in a timely fashion.

我尝试将 LocalService 帐户更改为 LocalSystem,但没有成功。 然后我也尝试更改主要功能

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
        { 
             new Service1() 
        };
ServiceBase.Run(ServicesToRun);

当我安装util convertor.exe时,它成功安装并启动了服务(但我需要通过程序启动它)。

为什么通过 installutil 安装时会启动服务,为什么手动调用 installhelper 时会抛出异常?

【问题讨论】:

    标签: c# windows-services installutil managedinstallerclass


    【解决方案1】:

    尝试删除服务中OnStart函数中的所有内容,看是否启动成功,如果启动则调试onStart函数(可以使用Debugger.Attach())

    祝你好运

    【讨论】:

    • 没用。同样的问题:(我仍然从 onstart 和 onstop 方法中删除了所有内容..
    • 尝试启动你的服务 OnCommitted 而不是 onAfterInstall
    • 我也试过这样。另外我尝试覆盖installer.cs的安装功能,但都没有成功。
    • 如果去掉 service start (temporary) 服务会安装成功吗?安装后能成功启动吗?
    • 是的。当我注释掉 servicecontroller.start() 时它成功安装但当我尝试从 services.msc 启动它时它失败并说:服务没有及时响应。但是,当我通过 installutil 安装服务时,它会在安装后自动启动。
    猜你喜欢
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2017-12-17
    • 2016-04-29
    • 1970-01-01
    • 2012-09-03
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多