2011-01-31 194 views
1

即时通讯尝试通过C#编程式安装服务,但遇到了一个我无法解决的问题。以编程方式安装Windows服务

在阅读大量文档后,我在那个我认为微软有bug的地方(但我们都知道情况并非如此)。我的申请是Main

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 }); 
       Console.Read(); 
       break; 
      case "/uninstall": 
       ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location }); 
       break; 
     } 
    } 
    else 
    { 
     ServiceBase.Run(new ProxyMonitor()); 
    } 
} 

像这样ProxyMonitor /install下的管理权限内CMD当执行步入下降到行:

ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 

不如预期,然后跳进我的安装类,像这样:

namespace Serco.Services.ProxyMonitor 
{ 
    [RunInstaller(true)] 
    public class ManagedInstallation : ServiceInstaller 
    { 
     public ManagedInstallation() 
     { 
      var ProcessInstaller = new ServiceProcessInstaller(); 
      var ServiceInstaller = new ServiceInstaller(); 

      //set the information and privileges 
      ProcessInstaller.Account  = ServiceConfiguration.AccountType; 
      ServiceInstaller.DisplayName = ServiceConfiguration.DisplayName; 
      ServiceInstaller.StartType  = ServiceConfiguration.StartType; 
      ServiceInstaller.Description = ServiceConfiguration.Description; 
      ServiceInstaller.ServiceName = ServiceConfiguration.ServiceName; 

      Installers.Add(ProcessInstaller); 
      Installers.Add(ServiceInstaller); 
     } 
    } 
} 

检查调试文件后,我得到以下内容:

Installing assembly 'C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe'. 
Affected parameters are: 
    logtoconsole = 
    logfile = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.InstallLog 
    assemblypath = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe 
Installing service ... 
Creating EventLog source in log Application... 
Rolling back assembly 'C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe'. 
Affected parameters are: 
    logtoconsole = 
    logfile = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.InstallLog 
    assemblypath = C:\Users\Robert\documents\visual studio 2010\Projects\ProxyMonitor\ProxyMonitor\bin\Debug\ProxyMonitor.exe 
Restoring event log to previous state for source . 

我也得到了以下调用内抛出的异常:

ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location }); 

指出:

安装失败,并回滚已执行。必须指定源的值。

任何想法?


更新:

配置类

namespace Serco.Services.ProxyMonitor 
{ 
    class ServiceConfiguration 
    { 
     public static string DisplayName 
     { 
      get { return "Serco Proxy Monitor"; } 
     } 

     public static string ServiceName 
     { 
      get { return "Serco Proxy Monitor"; } 
     } 

     public static string Description 
     { 
      get 
      { 
       return "Serco ProxyMonitor is a helper developed to manage the state of the proxy for the employess whilst of the internal network."; 
      } 
     } 

     public static ServiceStartMode StartType 
     { 
      get{return ServiceStartMode.Automatic;} 
     } 

     public static ServiceAccount AccountType 
     { 
      get{return ServiceAccount.LocalSystem;} 
     } 

     /*.. /Snip/ ..*/ 
    } 
} 
+0

您是否以管理员权限运行?此外,如果您创建Windows Service项目和安装程序以查看生成的确切代码,它可能会有所帮助,这可能是您尝试手动编写的内容,尽管可能以不同方式排列。 – 2011-01-31 21:39:23

+0

Im以管理员身份登录,并声明我在管理员模式下运行,并开始在VS 2010中作为空白项目。 – RobertPitt 2011-01-31 21:41:31

回答

2

它看起来像数源为null;你确定ServiceConfiguration.ServiceName被定义并有一个值吗?

+0

是的,我确定,我已将配置类添加到OP – RobertPitt 2011-01-31 21:43:59

4

我想通了,并认为我会张贴包装其他人可能会有同样的问题。

那是几件事情的组合,但生病只是迅速告诉你他们:

public static string ServiceName 
{ 
    get { return "Serco Proxy Monitor"; } 
} 
  • 必须成为return "SercoProxyMonitor";由于空间
  • 删除了UnhandledException然后深入堆栈显示更多跟踪
  • 需要具有完全管理员权限。

我认为主要的问题是,ServiceInstaller是使用ServiceName创建和EventLogSource,而作为EventLogSource中有空格,被扔一个合适的。