2015-10-07 34 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

对不起,花了这么长时间,但我终于能够测试这个,它工作得很好。 –

相关问题