Dot-Net

如何在安裝後立即啟動 .NET Windows 服務?

  • July 28, 2009

除了 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.

您可以在服務執行檔中執行所有這些操作,以響應從 InstallUtil 程序觸發的事件。覆蓋 OnAfterInstall 事件以使用 ServiceController 類來啟動服務。

<http://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceinstaller.aspx>

我在此處發布了使用 C# 創建 Windows 服務的分步過程。聽起來您至少到了這一點,現在您想知道安裝後如何啟動該服務。將 StartType 屬性設置為 Automatic 將導致服務在重新啟動系統後自動啟動,但它不會(正如您所發現的那樣)在安裝後自動啟動您的服務。

我不記得我最初在哪裡找到它(也許是 Marc Gravell?),但我確實找到了一個線上解決方案,允許您通過實際執行服務本身來安裝和啟動服務。這是一步一步的:

  1. 像這樣建構Main()服務的功能:
static void Main(string[] args)
{
   if (args.Length == 0) {
       // Run your service normally.
       ServiceBase[] ServicesToRun = new ServiceBase[] {new YourService()};
       ServiceBase.Run(ServicesToRun);
   } else if (args.Length == 1) {
       switch (args[0]) {
           case "-install":
               InstallService();
               StartService();
               break;
           case "-uninstall":
               StopService();
               UninstallService();
               break;
           default:
               throw new NotImplementedException();
       }
   }
}
  1. 這是支持程式碼:
using System.Collections;
using System.Configuration.Install;
using System.ServiceProcess;

private static bool IsInstalled()
{
   using (ServiceController controller = 
       new ServiceController("YourServiceName")) {
       try {
           ServiceControllerStatus status = controller.Status;
       } catch {
           return false;
       }
       return true;
   }
}

private static bool IsRunning()
{
   using (ServiceController controller = 
       new ServiceController("YourServiceName")) {
       if (!IsInstalled()) return false;
       return (controller.Status == ServiceControllerStatus.Running);
   }
}

private static AssemblyInstaller GetInstaller()
{
   AssemblyInstaller installer = new AssemblyInstaller(
       typeof(YourServiceType).Assembly, null);
   installer.UseNewContext = true;
   return installer;
}
  1. 繼續支持程式碼…
private static void InstallService()
{
   if (IsInstalled()) return;

   try {
       using (AssemblyInstaller installer = GetInstaller()) {
           IDictionary state = new Hashtable();
           try {
               installer.Install(state);
               installer.Commit(state);
           } catch {
               try {
                   installer.Rollback(state);
               } catch { }
               throw;
           }
       }
   } catch {
       throw;
   }
}

private static void UninstallService()
{
   if ( !IsInstalled() ) return;
   try {
       using ( AssemblyInstaller installer = GetInstaller() ) {
           IDictionary state = new Hashtable();
           try {
               installer.Uninstall( state );
           } catch {
               throw;
           }
       }
   } catch {
       throw;
   }
}

private static void StartService()
{
   if ( !IsInstalled() ) return;

   using (ServiceController controller = 
       new ServiceController("YourServiceName")) {
       try {
           if ( controller.Status != ServiceControllerStatus.Running ) {
               controller.Start();
               controller.WaitForStatus( ServiceControllerStatus.Running, 
                   TimeSpan.FromSeconds( 10 ) );
           }
       } catch {
           throw;
       }
   }
}

private static void StopService()
{
   if ( !IsInstalled() ) return;
   using ( ServiceController controller = 
       new ServiceController("YourServiceName")) {
       try {
           if ( controller.Status != ServiceControllerStatus.Stopped ) {
               controller.Stop();
               controller.WaitForStatus( ServiceControllerStatus.Stopped, 
                    TimeSpan.FromSeconds( 10 ) );
           }
       } catch {
           throw;
       }
   }
}
  1. 此時,在目標機器上安裝服務後,只需從命令行(與任何普通應用程序一樣)使用-install命令行參數執行服務即可安裝並啟動服務。

我想我已經涵蓋了所有內容,但是如果您發現這不起作用,請告訴我,以便我更新答案。

引用自:https://stackoverflow.com/questions/1195478