c# - 以编程方式安装 Windows 服务

标签 c# service managedinstallerclass

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

在阅读了大量文档之后,我认为 Microsoft 存在错误(但我们都知道情况并非如此)。

所以这是我的应用程序的 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());
    }
 }

当在管理权限下在 CMD 中执行时,如 ProxyMonitor/install 进入的步骤如下:

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 });

说明:

The installation failed, and the rollback has been performed. Must specify value for source.


更新:

配置类

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/ ..*/
    }
}

最佳答案

我想通了并认为我会发帖以防其他人可能遇到同样的问题。

这是几件事的结合,但我会快速向您展示:

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

我认为主要问题是 ServiceInstaller 使用 ServiceName 创建和 EventLogSource,因为 中有空格code>EventLogSource 它正在发作。

关于c# - 以编程方式安装 Windows 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4856403/

相关文章:

c# - 更新面板问题

c# - C# 中的 SQL 查询不支持希伯来语

node.js - Node 、要求、单例还是非单例?

c# - Windows服务通过ManagedInstallerClass启动失败,通过InstallUtil启动成功

c# - 线程数组c#

c# - 返回中间字母或回文字母的函数

hibernate - 执行 Grails 服务的 Gradle 任务

python - 如何使 Python 脚本(作为服务安装)在注销后仍然存在?

c# - 如何在卸载时删除应用程序文件夹