Main
クラスのメソッドProgram
を次のように変更します。
/// <summary>
/// The main entry point for the application.
/// </summary>
private static void Main()
{
var myService = new MyService();
if (Environment.UserInteractive)
{
Console.WriteLine("Starting service...");
myService.Start();
Console.WriteLine("Service is running.");
Console.WriteLine("Press any key to stop...");
Console.ReadKey(true);
Console.WriteLine("Stopping service...");
myService.Stop();
Console.WriteLine("Service stopped.");
}
else
{
var servicesToRun = new ServiceBase[] { myService };
ServiceBase.Run(servicesToRun);
}
}
Start
サービス クラスにメソッドを追加する必要があります。
public void Start()
{
OnStart(new string[0]);
}
プロジェクトのプロパティの「アプリケーション」タブで、プロジェクトの出力タイプを「Windows アプリケーション」ではなく「コンソール アプリケーション」に変更します。F5 キーを押すだけでデバッグを開始できますが、実行可能ファイルを Windows サービスとして実行することもできます。