Installazione di un servizio Windows a livello di programmazione
-
27-10-2019 - |
Domanda
Sto tentando di installare un servizio a livello di programmazione tramite C# ma ho riscontrato un problema che non riesco a aggirare.
Dopo aver letto un sacco di documentazione, sono a quel punto in cui credo che Microsoft abbia un bug (ma sappiamo tutti che non è così).
Quindi ecco il Main
della mia applicazione.
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());
}
}
Se eseguito all'interno di CMD sotto i privilegi di amministrazione come così ProxyMonitor /install
Lo step va alla linea:
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
Come previsto, e poi salta nella mia lezione di installazione in questo modo:
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);
}
}
}
Dopo aver controllato il file di debug ottengo quanto segue:
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 .
Viene anche lanciato l'eccezione nella seguente chiamata:
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
Affermando:
L'installazione non è riuscita e il rollback è stato eseguito. Deve specificare il valore per l'origine.
Aggiornamenti:
Classe di configurazione
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/ ..*/
}
}
Soluzione
Sembra che la fonte del registro sia nulla; sei sicuro che questo ServiceConfiguration.ServiceName
è definito e ha un valore?
Altri suggerimenti
L'ho capito e ho pensato che avrei pubblicato un encase altri potrebbero avere lo stesso problema.
Era una combinazione di alcune cose ma malato ti mostrano rapidamente:
public static string ServiceName
{
get { return "Serco Proxy Monitor"; }
}
- Doveva diventare
return "SercoProxyMonitor";
A causa degli spazi - Rimosso il
UnhandledException
che poi mostrava tracce di stack più approfondita - Necessario per avere diritti di amministratore completo.
Penso che il problema principale fosse che il ServiceInstaller
stava usando il ServiceName
per creare e EventLogSource
, E come c'erano spazi all'interno del EventLogSource
Stava lanciando una vestibilità.