以编程方式安装Windows服务

Rob*_*itt 1 c# service managedinstallerclass

我正在尝试通过C#以编程方式安装服务,但我遇到了一个我无法解决的问题.

在阅读了大量文档之后我就认为微软有一个错误,(但我们都知道情况并非如此).

所以这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());
    }
 }
Run Code Online (Sandbox Code Playgroud)

如果在管理权限下在CMD中执行,ProxyMonitor /install则步骤进入下一行:

ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
Run Code Online (Sandbox Code Playgroud)

按预期方式,然后跳转到我的安装类,如下所示:

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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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 .
Run Code Online (Sandbox Code Playgroud)

我也在以下调用中抛出异常:

ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
Run Code Online (Sandbox Code Playgroud)

说明:

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


更新:

配置类

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/ ..*/
    }
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*itt 5

我想出来,并认为我会发布包装其他人可能有同样的问题.

这是一些事情的组合,但生病只是很快告诉你他们:

public static string ServiceName
{
    get { return "Serco Proxy Monitor"; }
}
Run Code Online (Sandbox Code Playgroud)
  • 不得不成为return "SercoProxyMonitor";由于空间
  • 删除UnhandledException后显示更深入的堆栈跟踪
  • 需要拥有完全管理员权限.

我认为主要的问题是ServiceInstaller使用了ServiceName创造和EventLogSource,并且因为EventLogSource它内部有空间正在投入合适.