プログラムでWindowsサービスをインストールします
-
27-10-2019 - |
質問
C#を介してプログラムでサービスをインストールしようとしていますが、回避できない問題に遭遇しました。
たくさんのドキュメントを読んだ後、私はマイクロソフトがバグを持っていると信じているその時点でです(しかし、私たちは皆そうではないことを知っています)。
だからここにあります 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 });
述べ:
インストールに失敗し、ロールバックが実行されました。ソースの値を指定する必要があります。
更新:
構成クラス
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/ ..*/
}
}
解決
ログソースはnullのようです。よろしいですか ServiceConfiguration.ServiceName
定義され、値がありますか?
他のヒント
私はそれを理解し、他の人が同じ問題を抱えている可能性があるかもしれないとEncaseを投稿すると思いました。
それはいくつかのものの組み合わせでしたが、病気はすぐにあなたにそれらを見せます:
public static string ServiceName
{
get { return "Serco Proxy Monitor"; }
}
- ならなければならなかった
return "SercoProxyMonitor";
スペースのため - 削除しました
UnhandledException
その後、より詳細なスタックトレースを示しました - 完全な管理者の権利が必要でした。
主な問題は、それだと思います ServiceInstaller
を使用していました ServiceName
作成します EventLogSource
, 、そして内部にスペースがあったので EventLogSource
それはフィットを投げていました。
所属していません StackOverflow