除了service.StartType = ServiceStartMode.Automatic之外,我的服务在安装后不会启动
解决方案
在我的ProjectInstaller上插入了这段代码
protected override void OnAfterInstall(System.Collections.IDictionary savedState)
{
base.OnAfterInstall(savedState);
using (var serviceController = new ServiceController(this.serviceInstaller1.ServiceName, Environment.MachineName))
serviceController.Start();
}
感谢ScottTx和Francis B。
发布于 2009-07-28 17:42:14
您可以在服务可执行文件中执行所有这些操作,以响应从InstallUtil流程激发的事件。重写OnAfterInstall事件以使用ServiceController类启动服务。
http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx
发布于 2009-07-28 17:45:12
我已经发布了在C# here中创建Windows服务的逐步过程。听起来您至少到了这一步,现在您想知道如何启动安装后的服务。将StartType属性设置为Automatic将导致服务在重新启动系统后自动启动,但它不会(如您所发现的)在安装后自动启动服务。
我不记得我最初是在哪里找到它的(也许是Marc Gravell?),但我确实在网上找到了一个解决方案,它允许您通过实际运行服务本身来安装和启动您的服务。下面是分步操作:
Main()
函数构造如下:static void Main(string[] args) { if (args.Length == 0) { //正常运行您的服务。ServiceBase[] ServicesToRun = new ServiceBase[] {new YourService()};ServiceBase.Run(ServicesToRun);} else if (args.Length == 1) { switch (args) { case "-install":InstallService();StartService();break;case "-uninstall":StopService();UninstallService();break;default:抛出新的NotImplementedException();}
使用System.Collections;使用System.Configuration.Install;使用System.ServiceProcess;私有静态布尔IsInstalled() { using (ServiceController控制器=新ServiceController("YourServiceName")) { try { ServiceControllerStatus status = controller.Status;} catch { return false;} return true;}}私有静态bool IsRunning() { using (ServiceController ServiceController=新类型(“YourServiceName”)){ if (!IsInstalled()) return false;return (controller.Status == ServiceControllerStatus.Running);}}私有静态安装程序(){ AssemblyInstaller installer =新AssemblyInstaller( typeof(YourServiceType).Assembly,null);installer.UseNewContext = true;return installer;}
私有静态空InstallService() { if (IsInstalled()) return;try { using (AssemblyInstaller installer = GetInstaller()) { IDictionary状态=新哈希表();try {installer.Install(状态);installer.Commit(状态);} catch { try {installer.Rollback(状态);} catch {} throw;} catch { throw;}}私有静态空UninstallService() { if ( !IsInstalled() ) return;try { using ( AssemblyInstaller installer = GetInstaller() ){ IDictionary state = new Hashtable();尝试{ installer.Uninstall(状态);}捕获{抛出;}}捕获{抛出;}}私有静态空StartService() { if ( !IsInstalled() )返回;使用(ServiceController控制器=新ServiceController("YourServiceName")) { try { if ( controller.Status != ServiceControllerStatus.Running ){ controller.Start();controller.WaitForStatus( ServiceControllerStatus.Running,TimeSpan.FromSeconds( 10 ) );}}捕获{抛出;}私有静态空StopService() { if ( !IsInstalled() ) return;using ( ServiceController ServiceController=新ServiceController("YourServiceName")) { try { if ( controller.Status != ServiceControllerStatus.Stopped ){ controller.Stop();controller.WaitForStatus( ServiceControllerStatus.Stopped,TimeSpan.FromSeconds( 10 ) );}} catch {
-install
命令行参数从命令行运行服务(就像任何普通应用程序一样),即可安装并启动服务。我想我已经涵盖了所有的内容,但是如果你发现这不起作用,请让我知道,这样我就可以更新答案。
发布于 2009-07-28 17:38:05
Visual Studio
如果您正在使用VS创建安装项目,则可以创建一个调用.NET方法来启动服务的自定义操作。但是,实际上并不建议在MSI中使用托管自定义操作。请参阅此page。
ServiceController controller = new ServiceController();
controller.MachineName = "";//The machine where the service is installed;
controller.ServiceName = "";//The name of your service installed in Windows Services;
controller.Start();
InstallShield或Wise
如果您使用的是InstallShield或Wise,这些应用程序提供了启动服务的选项。根据Wise的示例,您必须添加一个服务控制操作。在此操作中,您可以指定是要启动还是要停止服务。
Wix
使用Wix,您需要在服务的组件下添加以下xml代码。有关这方面的更多信息,请查看此page。
<ServiceInstall
Id="ServiceInstaller"
Type="ownProcess"
Vital="yes"
Name=""
DisplayName=""
Description=""
Start="auto"
Account="LocalSystem"
ErrorControl="ignore"
Interactive="no">
<ServiceDependency Id="????"/> ///Add any dependancy to your service
</ServiceInstall>
https://stackoverflow.com/questions/1195478
复制相似问题