安装时自动启动 Windows 服务

发布于 2024-08-21 03:31:30 字数 467 浏览 8 评论 0原文

我有一个 Windows 服务和一个 MSI 安装程序(安装项目)。安装项目具有用于安装和卸载的自定义操作,参数分别为 /install 和 /uninstall。

我希望该服务在安装后立即启动。我的服务所做的就是启动一个进程。当服务停止时,它会执行process.Close();

我尝试过执行

var sc = new ServiceController( "MyProcess" );
sc.Start();

进程启动,但未显示表示服务已启动的事件日志消息。停止时,我收到错误错误停止进程:System.InvalidOperationException:没有进程与此对象关联。

如果我不使用服务控制器来启动并使用 Services.msc,则停止时它会按预期工作。

有没有办法让进程立即启动并使我的启动/停止方法按预期工作?

I have a Windows service and an MSI installer (setup project) for it. The setup project has custom actions for install and uninstall with args of /install and /uninstall respectively.

I would like the service to start immediately after the install. All my service does is starts a process. When the service is stopped, it does process.Close();.

I have tried doing

var sc = new ServiceController( "MyProcess" );
sc.Start();

The process starts, but the event log message saying the service has started doesn't show. When stopping I get the error Error stopping process: System.InvalidOperationException: No process is associated with this object..

If I don't use the service controller to start and use Services.msc instead, it works as expected when stopping.

Is there a way to have the process start immediately and have my start/stop methods work as expected?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

行雁书 2024-08-28 03:31:30

你没有说你正在使用什么操作系统,但我记得去年开发安装程序应用程序时遇到了这个问题。我相信这是 Windows 7、Server 2003/2008 和可能 Vista 中的安全限制,因为安装程序无法启动它安装的任何应用程序。如果您将服务设置为自动启动,那么它应该在下次系统重新启动时启动。

You don't say what operating system you are using, but I remember running into this when developing an installer app last year. I beleive this is a security restriction in Windows 7, Server 2003/2008, and possibly Vista, as installation program cannot start any application program that it installs. If you set the service for Automatic Startup, then it should start the next time the system restarts.

我乃一代侩神 2024-08-28 03:31:30

我认为你所说的是这里存在两个问题。第一个问题是该服务在安装时不会启动。二是服务无法启动进程。我说得对吗?

当我通过安装项目安装服务时,我总是从安装程序类的 Commit 方法启动服务,这样我就知道一切都已正确安装。我对此不是 100% 确定,但在提交安装之前您可能无法启动该服务。看起来您的自定义操作设置正确,因此调用 Commit 方法应该不会有任何问题(您确实将 Commit 包含在自定义操作中,对吧?)以下是来自一个的 Commit 方法的示例我的项目:

public override void Commit(IDictionary savedState)
{
    base.Commit(savedState);

    try
    {
        var serviceController = new ServiceController("<Insert Service Name>");
        serviceController.Start();
    }
    catch
    {
        MessageBox.Show(
            "There was an error starting the <Insert Service Name> Service. Please manually start it, or restart the computer.");
    }
}

通过向用户显示消息框,如果服务启动出现问题,至少会通知用户应该手动启动该服务。调试安装问题时,您可以在消息框中包含异常,以获取有关到底发生了什么的更多详细信息。

我很好奇 - 您在 ServiceProcessInstaller 中使用什么帐户?该用户是否有权在计算机上启动该进程?该流程是否有任何特定的 UAC 要求?我怀疑访问控制是阻止您的进程启动的原因。

I think what you're saying is that there are two issues are going on here. The first issue is that the service doesn't start upon install. The second is that the service is unable to start the process. Am I right?

When I've installed services through a setup project, I always start the service from the Commit method of the installer class, that way I know that everything got installed correctly. I'm not 100% sure about this, but you may not be able start the service until the installation has been committed. It looks like you have your Custom Actions set up correctly, so there shouldn't be any issue with the Commit method getting called (you do have Commit included in the Custom Actions, right?) Here is an example of the Commit method from one of my projects:

public override void Commit(IDictionary savedState)
{
    base.Commit(savedState);

    try
    {
        var serviceController = new ServiceController("<Insert Service Name>");
        serviceController.Start();
    }
    catch
    {
        MessageBox.Show(
            "There was an error starting the <Insert Service Name> Service. Please manually start it, or restart the computer.");
    }
}

By showing the message box to the user, if something goes wrong with the service start, at least the user will be notified that the service should be started manually. When debugging the install issue, you can include the exception in the message box for more details into what exactly is going on.

I'm curious - what Account are you using in your ServiceProcessInstaller? Does this user have the rights on the computer to start the process? Does the Process have any specific UAC requirements? I suspect that Access Control is what is keeping your process from starting.

〆凄凉。 2024-08-28 03:31:30

我最终采取了一种不同的方法,只获取正在运行的进程,而不是试图保留之前开始的进程。然后停止进程就可以正常工作,并且 OnStart/OnStop 方法是否“正常”运行并不重要,因为我的内部进程正在按照我的需要启动/停止。

I ended up taking a different approach by just getting the running process instead of trying to hold on to the one started before. Stopping the process then works just fine, and doesn't matter of the OnStart/OnStop methods are functioning "properly", because my inner process is starting/stopping like I need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文