それは実際には非常に簡単です。私は自分のサービスの多くでそれを使用しました (実際、私のサービスはすべて独自のインストール/アンインストールを実行できます。/install
またはなどのコマンドライン スイッチで制御し/uninstall
ます。
インストールは次のように実行されます。
private static void InstallService()
{
var ti = new System.Configuration.Install.TransactedInstaller();
var si = new MyServiceInstaller();
var cl = new string[] { string.Format(CultureInfo.InvariantCulture, "/assemblypath={0}", System.Reflection.Assembly.GetExecutingAssembly().Location) };
var ctx = new System.Configuration.Install.InstallContext(null, cl);
ti.Installers.Add(si);
ti.Context = ctx;
ti.Install(new Hashtable());
}
ti.Uninstall(null);
の代わりにを呼び出す点を除いて、アンインストールは同じですti.Install(...);
。
MyMyServiceInstaller
は、クラスから継承するSystem.Configuration.Install.Installer
クラスです (通常はサービスにあるように)。