C# 以编程方式安装windows服务

C# 以编程方式安装windows服务,c#,service,managedinstallerclass,C#,Service,Managedinstallerclass,我试图通过C语言以编程方式安装服务,但遇到了一个无法解决的问题 在阅读了大量文档之后,我认为微软有一个bug,但我们都知道事实并非如此 这是我申请的主要部分 static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomainUnhandledException; if (System.Environment.UserInteractive) {

我试图通过C语言以编程方式安装服务,但遇到了一个无法解决的问题

在阅读了大量文档之后,我认为微软有一个bug,但我们都知道事实并非如此

这是我申请的主要部分

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内以管理权限(如so 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 });
声明:

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

更新:

配置类
看起来日志源为空;是否确实定义了ServiceConfiguration.ServiceName并具有值?

日志源似乎为空;您确定ServiceConfiguration.ServiceName已定义且具有值吗?

我已经计算出了它,并认为我会将其后期封装。其他人可能也有相同的问题

这是一些东西的组合,但我会很快向您展示:

public static string ServiceName
{
    get { return "Serco Proxy Monitor"; }
}
必须成为回报的首席检察官;由于空间的原因 删除了未处理的异常,该异常随后显示了更深入的堆栈跟踪 需要具有完全的管理员权限。
我认为主要的问题是ServiceInstaller使用ServiceName来创建和运行EventLogSource,而EventLogSource中有空格,这让它很恼火。

我发现了这个问题,并认为我会将其他人可能也有同样的问题后封装

这是一些东西的组合,但我会很快向您展示:

public static string ServiceName
{
    get { return "Serco Proxy Monitor"; }
}
必须成为回报的首席检察官;由于空间的原因 删除了未处理的异常,该异常随后显示了更深入的堆栈跟踪 需要具有完全的管理员权限。
我认为主要的问题是ServiceInstaller使用ServiceName创建和运行EventLogSource,并且由于EventLogSource中有空格,因此引发了冲突。

您是否以管理员权限运行?另外,如果您创建一个Windows服务项目和安装程序来查看生成的确切代码,这可能会有所帮助,这将是您试图手动编写的代码,尽管可能会有不同的安排。我以管理员身份登录,并声明我是在管理员模式下运行的,我在VS2010开始时是一个空白项目。你是以管理员权限运行的吗?此外,如果您创建一个Windows服务项目和安装程序来查看生成的确切代码,这可能会有所帮助,这将是您试图手动编写的代码,尽管可能会有不同的安排。我以管理员身份登录,并声明我是在管理员模式下运行的,我在VS 2010中以空白项目的形式启动。是的,我确信,我已将config类附加到OPThis had lead以获得正确答案,因此我将其标记为正确。是的,我确信,我已将config类附加到OPThis had lead以获得正确答案,因此我将其标记为正确。