1

C# を使用してプログラムでサービスをインストールしようとしていますが、回避できない問題に遭遇しました。

たくさんのドキュメントを読んだ結果、Microsoft にはバグがあると思うところまで来ました (しかし、そうではないことは誰もが知っています)。

これ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/ ..*/
    }
}
4

2 に答える 2

5

私はそれを理解し、他の人が同じ問題を抱えている可能性がある場合に投稿すると思いました.

これはいくつかの組み合わせでしたが、簡単に説明します。

public static string ServiceName
{
    get { return "Serco Proxy Monitor"; }
}
  • return "SercoProxyMonitor";スペースが原因でなくてはならなかった
  • を削除し、UnhandledException詳細なスタック トレースを表示しました
  • 完全な管理者権限が必要です。

主な問題は、ServiceInstallerが を使用してServiceNameと を作成していたことEventLogSourceであり、 の中にスペースがEventLogSourceあったため、うまくいきませんでした。

于 2011-02-01T09:26:51.347 に答える
2

ログ ソースが null のようです。ServiceConfiguration.ServiceNameが定義されており、値があると確信していますか?

于 2011-01-31T21:41:06.070 に答える