Programmatisch installieren eines Windows -Dienstes
-
27-10-2019 - |
Frage
Ich versuche, einen Dienst programmgesteuert über C# zu installieren, aber ich bin auf ein Problem gestoßen, das ich nicht umgehen kann.
Nachdem ich eine Menge Dokumentation gelesen habe, bin ich an diesem Punkt, an dem ich glaube, dass Microsoft einen Fehler hat (aber wir alle wissen, dass dies nicht der Fall ist).
Also hier ist das Main
meiner Bewerbung.
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());
}
}
Wenn Sie innerhalb von CMD unter Verwaltungsberechtigten ausgeführt werden ProxyMonitor /install
Der Schritt in die Linie geht auf die Linie:
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
Wie erwartet und springt dann in meine Installationsklasse wie 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);
}
}
}
Nachdem ich die Debug -Datei überprüft habe, erhalte ich Folgendes:
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 .
Ich bekomme auch die Ausnahme in den folgenden Anruf:
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
Angabe:
Die Installation ist fehlgeschlagen und der Rollback wurde durchgeführt. Muss einen Wert für die Quelle angeben.
Aktualisierung:
Konfigurationsklasse
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/ ..*/
}
}
Lösung
Es sieht so aus, als ob die Protokollquelle null ist; sind Sie sicher, dass ServiceConfiguration.ServiceName
ist definiert und hat einen Wert?
Andere Tipps
Ich habe es herausgefunden und dachte, ich würde eine Einschließung veröffentlichen, die andere möglicherweise das gleiche Problem haben.
Es war eine Kombination aus ein paar Dingen, aber ich zeige Ihnen sie nur schnell:
public static string ServiceName
{
get { return "Serco Proxy Monitor"; }
}
- Musste werden
return "SercoProxyMonitor";
Aufgrund der Räume - Entfernte
UnhandledException
die dann mehr in Tiefenstapelspuren zeigten - Mussten volle Administratorrechte haben.
Ich denke, das Hauptproblem war, dass die ServiceInstaller
benutzte die ServiceName
Erstellen und EventLogSource
, Und wie es Räume innerhalb der gab EventLogSource
Es warf eine Passform.