安装程序完成后自动启动服务

发布于 2024-12-12 09:58:52 字数 1134 浏览 0 评论 0原文

可能的重复:
如何在安装后自动启动服务?

我有一个在 Windows 7 x64 上运行的 Visual Studio 2008 C# .NET 3.5 服务安装程序项目 (MSI)。

我订阅 ServiceInstaller.OnAfterInstall 通知,以便在安装完成后启动我的服务。

[RunInstaller(true)]
public partial class MyInstaller : Installer
{
    private System.ServiceProcess.ServiceInstaller my_installer_;

    private void InitializeComponent()
    {
        // ...
        this.my_installer_.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.OnAfterInstall);
        // ...
    }

    private void OnAfterInstall(object sender, InstallEventArgs e)
    {
        using (System.ServiceProcess.ServiceController svc =
            new System.ServiceProcess.ServiceController("MyService"))
        {
            svc.Start(); // completes successfully
        }
    }
}

尽管该函数毫无例外地成功,但当安装程序完成时,我的服务从未运行。

事件日志显示没有与服务启动相关的失败,如果我进入服务管理器,我可以手动启动该服务(或重新启动电脑,它将自动启动)。

安装程序完成后,我需要做什么才能自动启动我的服务?

Possible Duplicate:
How to automatically start your service after install?

I have a Visual Studio 2008 C# .NET 3.5 service installer project (MSI) running on Windows 7 x64.

I subscribe to the ServiceInstaller.OnAfterInstall notification to start my service when the installation finishes.

[RunInstaller(true)]
public partial class MyInstaller : Installer
{
    private System.ServiceProcess.ServiceInstaller my_installer_;

    private void InitializeComponent()
    {
        // ...
        this.my_installer_.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.OnAfterInstall);
        // ...
    }

    private void OnAfterInstall(object sender, InstallEventArgs e)
    {
        using (System.ServiceProcess.ServiceController svc =
            new System.ServiceProcess.ServiceController("MyService"))
        {
            svc.Start(); // completes successfully
        }
    }
}

Though the function succeeds without exception, my service is never running when the installer finishes.

The event log shows no failures related to service startup and if I go to the services manager, I can start the service manually (or restart the PC and it will start automatically).

What do I need to do to automatically start my service when the installer process finishes?

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

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

发布评论

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

评论(2

兮子 2024-12-19 09:58:53

使用 AfterInstall 事件

在 Service Installer 类中创建 AfterInstall 事件并使用 ServiceController 启动服务。

public ServiceInstaller()
{
    InitializeComponent();
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

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

使用提交事件

public ServiceInstaller()
{
    InitializeComponent();
    this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}

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

或者您可以覆盖 OnCommited 事件

    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
        base.OnCommitted(savedState);
        new ServiceController(serviceInstaller1.ServiceName).Start();
    }

除上述之外,请检查以下

  • 安装程序启动类型:自动
  • 帐户:本地系统

除了服务安装程序之外,您还需要安装项目通过提供上述服务安装程序的主要输出来创建。

在此处输入图像描述

在设置中至少在安装时通过提供服务安装程序项目输出来创建自定义操作。

在此处输入图像描述

来自 此处

Using AfterInstall event

Create AfterInstall event in your Service Installer class and start service using ServiceController.

public ServiceInstaller()
{
    InitializeComponent();
    this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
}

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

Using Committed event

public ServiceInstaller()
{
    InitializeComponent();
    this.Committed += new InstallEventHandler(ProjectInstaller_Committed);
}

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

Or you can override OnCommitted event

    protected override void OnCommitted(System.Collections.IDictionary savedState)
    {
        base.OnCommitted(savedState);
        new ServiceController(serviceInstaller1.ServiceName).Start();
    }

Other than above please check following

  • Installer Start type : Automatic
  • Account :Local System

Other than the service installer you need to have setup project which created by giving primary output of above service installer.

enter image description here

in the setup create Custom action at least on install by giving service installer project output.

enter image description here

More information from here.

得不到的就毁灭 2024-12-19 09:58:53

我假设 Start 立即返回,并在后台启动服务。检查文档: http://msdn.microsoft.com/en-us/library /yb9w7ytd.aspx

I assume that Start returns immediatly, and Starts the Service in the background. Check the Docs: http://msdn.microsoft.com/en-us/library/yb9w7ytd.aspx

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