2010-11-10 53 views
35

我已阅读此问题Inno Setup for Windows service?。我有同样的问题,但我不明白从lubos hasko的答案。我该如何做到这一点?你有人可以发布我完整的演练吗?在.NET中自行安装windows服务c#

当我运行下面的代码时,会安装一些东西,但在服务列表中,我找不到它。 我有这个,但是这不工作:

using System; 
using System.Collections.Generic; 
using System.Configuration.Install; 
using System.Linq; 
using System.Reflection; 
using System.ServiceProcess; 
using System.Text; 
using System.IO; 

namespace ConsoleApplication1 
{ 

public class Service1 : ServiceBase 
{ 

    public Service1() 
    { 
     File.AppendAllText("sss.txt", "ccccc"); 
    } 

    protected override void OnStart(string[] args) 
    { 
     File.AppendAllText("sss.txt", "asdfasdf"); 
    } 

    protected override void OnStop() 
    { 
     File.AppendAllText("sss.txt", "bbbbb"); 
    } 


    static void Main(string[] args) 
    { 
     if (System.Environment.UserInteractive) 
     { 
      string parameter = string.Concat(args); 
      switch (parameter) 
      { 
       case "--install": 
        ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 
        break; 
       case "--uninstall": 
        ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); 
        break; 
      } 
     } 
     else 
     { 
      ServiceBase.Run(new Service1()); 
     } 


     Console.ReadKey(); 
    } 
} 
} 

我不understad这两种:

if (System.Environment.UserInteractive) ... 

回答

72

这是我完整的解决方案,它的工作原理。这与this问题基本相同。

using System; 
using System.Configuration.Install; 
using System.Reflection; 
using System.ServiceProcess; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class Program : ServiceBase 
    { 
     static void Main(string[] args) 
     { 

      AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; 


      if (System.Environment.UserInteractive) 
      { 
       string parameter = string.Concat(args); 
       switch (parameter) 
       { 
        case "--install": 
         ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 
         break; 
        case "--uninstall": 
         ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); 
         break; 
       } 
      } 
      else 
      { 
       ServiceBase.Run(new Program()); 
      } 



     } 

     private static void CurrentDomainUnhandledException(object sender, UnhandledExceptionEventArgs e) 
     { 
      File.AppendAllText(@"C:\Temp\error.txt", ((Exception)e.ExceptionObject).Message + ((Exception)e.ExceptionObject).InnerException.Message); 
     } 

     public Program() 
     { 
      this.ServiceName = "My Service"; 
      File.AppendAllText(@"C:\Temp\sss.txt", "aaa"); 

     } 

     protected override void OnStart(string[] args) 
     { 
      base.OnStart(args); 

      File.AppendAllText(@"C:\Temp\sss.txt", "bbb"); 
     } 

     protected override void OnStop() 
     { 
      base.OnStop(); 

      File.AppendAllText(@"C:\Temp\sss.txt", "ccc"); 
     } 
    } 
} 

在同一个项目中创建这个类:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Configuration.Install; 
using System.Linq; 
using System.ServiceProcess; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    [RunInstaller(true)] 
    public class MyWindowsServiceInstaller : Installer 
    { 
     public MyWindowsServiceInstaller() 
     { 
      var processInstaller = new ServiceProcessInstaller(); 
      var serviceInstaller = new ServiceInstaller(); 

      //set the privileges 
      processInstaller.Account = ServiceAccount.LocalSystem; 

      serviceInstaller.DisplayName = "My Service"; 
      serviceInstaller.StartType = ServiceStartMode.Automatic; 

      //must be the same as what was set in Program's constructor 
      serviceInstaller.ServiceName = "My Service"; 
      this.Installers.Add(processInstaller); 
      this.Installers.Add(serviceInstaller); 
     } 
    } 
} 

运行这个程序与参数--install/- 在Windows 7中检查卸载作为管理员错误日志中的温度。检查相同路径上的工作日志。

0

System.Environment.UserInteractive财产告诉你 是否Windows进程或如IIS服务,没有一个用户运行接口。

如果此属性为false,则不显示模式对话框或消息框,因为没有用于与用户交互的图形用户界面。 Source

检查this文章为好。

+0

谢谢,那篇文章真的帮了我。但在文章中,作者使用了installutil。我不想使用installutil。有什么选择吗?答案是在这篇文章http://stackoverflow.com/questions/1449994/inno-setup-for-windows-service/1450051#1450051但我不知道如何使用它。 – Simon 2010-11-10 13:47:29

+0

为什么你不想用installutil安装服务?如果是因为权限,您将无法在没有管理员权限的情况下安装服务。这没有工作。 – jlafay 2010-11-10 14:09:09

+0

其实,我仍然使用installutil,但是通过ManagedInstallerClass.InstallHelper。这才是重点。当我部署我的程序时,我也不需要部署installutil.exe。在使用Inno Setup安装应用程序期间安装WS,这是在管理员权限下进行的,因此没有问题... – Simon 2010-11-11 09:45:01

1

首先,在你的Service1的构造函数中设置ServiceName属性。从MSDN

摘录:

你需要在构造函数中从ServiceBase继承一个类来实现最小的是设置服务名称的组件。在构造函数中没有其他处理是特别需要的。您应该在OnStart中处理大多数初始化,而不是在构造函数中。

其次,在从命令行运行时,需要将参数传递给您的服务。 --install进行安装,--uninstall进行卸载 - 查看你的switch语句,它正在输入参数。

+0

是的,传递参数并修正了构造函数。仍然没有运气。 – Simon 2010-11-10 13:48:19

+0

Doh!请参阅Simon的回答,安装程序部分丢失。 – Mihailo 2010-11-10 14:58:54

+0

顺便说一下,在向项目添加新项目时,您可以选择Windows Service(右键单击项目,在弹出菜单中选择Add - > New Item ...并在其中找到Windows Service)。像这样添加后,您可以很好地设置服务的属性并添加安装程序等。 – Mihailo 2010-11-10 15:03:59