安装程序完成后自动启动服务
可能的重复:
如何在安装后自动启动服务?
我有一个在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 AfterInstall 事件
在 Service Installer 类中创建
AfterInstall
事件并使用ServiceController
启动服务。使用提交事件
或者您可以覆盖 OnCommited 事件
除上述之外,请检查以下
除了服务安装程序之外,您还需要安装项目通过提供上述服务安装程序的主要输出来创建。
在设置中至少在安装时通过提供服务安装程序项目输出来创建自定义操作。
来自 此处。
Using AfterInstall event
Create
AfterInstall
event in your Service Installer class and start service usingServiceController
.Using Committed event
Or you can override OnCommitted event
Other than above please check following
Other than the service installer you need to have setup project which created by giving primary output of above service installer.
in the setup create Custom action at least on install by giving service installer project output.
More information from here.
我假设 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