2015-10-07 25 views
1

如何將命令提示符應用程序轉換爲Windows服務?到目前爲止,這是我,但是當我嘗試使用InstallUtil.exe安裝它,我收到了一個錯誤:C#命令提示符應用程序到Windows服務 - 如何創建安裝程序類

與RunInstallerAttribute.Yes沒有公共屬性的安裝程序可在....

找到

我不知道我必須創建一個安裝程序類,我不知道如何去做這件事。有人能幫助我告訴我如何編寫安裝程序類,以便我可以將我的應用程序安裝爲Windows服務?

class Program 
    { 
    public const string ServiceName = "ProcessingApp"; 

    public class Service : ServiceBase 
    { 
     public Service() 
     { 
      ServiceName = Program.ServiceName; 
     } 

     protected override void OnStart(string[] args) 
     { 
      Program.Start(args); 
     } 

     protected override void OnStop() 
     { 
      Program.Stop(); 
     } 
    } 

    private static void Start(string[] args) 
    { 
     // onstart code here 
     StartCode(); 
    } 

    private static void Stop() 
    { 
     // onstop code here 

     ServiceController service = new ServiceController(ServiceName); 
     try 
     { 
      TimeSpan timeout = TimeSpan.FromMilliseconds(100000); 

      service.Stop(); 
      service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); 
     } 
     catch 
     { 

     } 

    } 

    static void Main(string[] args) 
    { 
     if (!Environment.UserInteractive) 
      // running as service 
      using (var service = new Service()) 
       ServiceBase.Run(service); 
     else 
     { 
      // running as console app 
      Start(args); 

      Console.WriteLine("Press any key to stop..."); 
      Console.ReadKey(true); 

      Stop(); 
     } 

回答

1

對於工作,你需要有一個從System.Configuration.Install.InstallerSystem.Configuration.Install.dll繼承的類。裏邊反構造應該配置一個ServiceProcessInstallerServiceInstaller,並把它們添加到收藏Installers - 設置AccountStartTypeServiceNameDescription

MSDN有一個例子:https://msdn.microsoft.com/en-us/library/system.serviceprocess.serviceprocessinstaller(v=vs.110).aspx

+0

我會嘗試了這一點明天早上謝謝 –

+0

我還沒有能夠嘗試這個呢。我的網絡人員花時間給我安裝它作爲管理員的能力。 –

+0

對不起,花了這麼長時間,但我終於能夠測試這個,它工作得很好。 –

相關問題