.net 如何在安装后自动启动服务?
如何在从Visual Studio安装项目运行安装后自动启动服务.net 如何在安装后自动启动服务?,.net,installation,service,.net,Installation,Service,如何在从Visual Studio安装项目运行安装后自动启动服务 我刚刚想出了这个答案,我想为了整体利益,我会和大家分享答案。答案如下。我对其他更好的方法持开放态度。将以下类添加到您的项目中 using System.ServiceProcess; class ServInstaller : ServiceInstaller { protected override void OnCommitted(System.Collections.IDictionary savedState
我刚刚想出了这个答案,我想为了整体利益,我会和大家分享答案。答案如下。我对其他更好的方法持开放态度。将以下类添加到您的项目中
using System.ServiceProcess;
class ServInstaller : ServiceInstaller
{
protected override void OnCommitted(System.Collections.IDictionary savedState)
{
ServiceController sc = new ServiceController("YourServiceNameGoesHere");
sc.Start();
}
}
安装程序完成后,安装项目将拾取类并运行您的服务。谢谢,运行正常
private System.ServiceProcess.ServiceInstaller serviceInstaller1;
private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
ServiceController sc = new ServiceController("YourServiceName");
sc.Start();
}
对公认答案的小补充: 您还可以这样获取服务名称-避免将来更改服务名称时出现任何问题:
protected override void OnCommitted(System.Collections.IDictionary savedState)
{
new ServiceController(serviceInstaller1.ServiceName).Start();
}
每个安装程序都有一个ServiceProcessInstaller和一个ServiceInstaller。此处,ServiceInstaller被称为serviceInstaller1。不要创建自己的类,而是在项目安装程序中选择服务安装程序,并向提交的事件添加事件处理程序:
private void serviceInstallerService1_Committed(object sender, InstallEventArgs e)
{
var serviceInstaller = sender as ServiceInstaller;
// Start the service after it is installed.
if (serviceInstaller != null && serviceInstaller.StartType == ServiceStartMode.Automatic)
{
var serviceController = new ServiceController(serviceInstaller.ServiceName);
serviceController.Start();
}
}
仅当启动类型设置为“自动”时,它才会启动您的服务。根据上面的代码片段,对于名为FSWServiceMgr.exe的服务,我的ProjectInstaller.cs文件的结尾如下所示。安装后服务确实启动了。请注意,在解决方案资源管理器中选择安装项目以设置公司时,请记住单击“属性”选项卡而不是右键单击,以此类推
这种方法使用安装程序类和最少的代码
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace MyProject
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
serviceInstaller1.AfterInstall += (sender, args) => new ServiceController(serviceInstaller1.ServiceName).Start();
}
}
}
在安装程序类设计器中定义serviceInstaller1类型ServiceInstaller,并在设计器中设置其ServiceName属性。还有另一种不涉及代码的方法。您可以使用服务控制表。使用orca.exe编辑生成的msi文件,并将条目添加到 只有ServiceControl、Name、Event和Component列是必填的。Component列包含文件表中的ComponentId。选择文件表中的文件,并将组件_值复制到ServiceControl表中 最后一步是在InstallExecutesequence表中将StartServices的值更新为6575。这足以启动服务
顺便说一下,服务安装表允许您配置安装程序来安装windows服务。我很高兴看到有人发布了一个有用的问题,他们知道答案。有时候你只知道你的小费会受到欢迎。也有可能有人会用一个有吸引力的替代方案来回答你的问题。这正是我所希望的。这显然是我想要做的事情。微软从他们的代码中漏掉的东西总是让我惊讶不已。ServiceController实现了IDisposable。不是故意使用'using'关键字或调用Dispose方法吗?我同意正确地处理总是一个好主意。在本例中,它只运行一次。OnCommitted在安装程序运行后启动,然后该服务像其他服务一样进行管理,并在下次重新启动时自动启动。base.OnCommitted。。。。是否需要调用它?您可以只使用Committed或AfterInstall事件,而不是创建新类。请参阅下面的答案。这实际上是最简单的方法,因为Service installer和AfterInstall事件已经存在-无需添加新类。不要忘记添加:使用System.ServiceProcess;这是最适合我的选项,而且似乎是最简单和最明显的ServiceInstaller1是ProjectInstaller的私有成员变量,因此我如何从ServiceInstaller访问它?我必须将此代码放在提交事件中。似乎我的服务在安装后还不可用。危险信号!所有这些都很好,而且可能很好地深入msi软件包进行学习,但这将成为发布更新时的负担,因为每次生成msi软件包时,您都必须手动执行所有这些操作。有人会在某个时候忘记这一点,如果你不推一段时间的更新,这一切都会被忘记。
using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;
namespace MyProject
{
[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
public ProjectInstaller()
{
InitializeComponent();
serviceInstaller1.AfterInstall += (sender, args) => new ServiceController(serviceInstaller1.ServiceName).Start();
}
}
}