I'm attempting to install a service programmatically via C# but I have run into an issue that I can't get around.

After reading loads of documentation I'm at that point where I believe Microsoft have a bug, (But we all know that's not the case).

So here's the Main of my application.

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

When executed within CMD under administration privileges like so ProxyMonitor /install the step into goes down to the line:

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

as expected, and then jumps into my install class like so:

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

After checking the debug file I get the following:

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 .

I also get the exception thrown within the following call:

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

Stating:

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


Updates:

Configuration Class

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/ ..*/
    }
}
有帮助吗?

解决方案

It looks like the log source is null; are you sure that ServiceConfiguration.ServiceName is defined and has a value?

其他提示

I figured it out and thought I would post encase others may be having the same issue.

It was a combination of a few things but ill just quickly show you them:

public static string ServiceName
{
    get { return "Serco Proxy Monitor"; }
}
  • Had to become return "SercoProxyMonitor"; due to the spaces
  • Removed the UnhandledException which then showed more in depth stack traces
  • Needed to have Full Administrator Rights.

I think the main issue was that the ServiceInstaller was using the ServiceName to create and EventLogSource, And as there were spaces within the EventLogSource it was throwing a fit.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top