To Create a Windows Service App,
1.Start Visual C# (Express)
2.Create New Project (Empty Project) + Add Reference (System.ServiceProcess, System.Configuration, System.Configuration.Install)
3.Create a Service Class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.ServiceProcess; namespace ServiceApp { class MyService : ServiceBase { public MyService(string[] args) { this.ServiceName = "MyService.Application"; this.EventLog.Log = "Application"; this.CanHandlePowerEvent = true; this.CanHandleSessionChangeEvent = true; this.CanPauseAndContinue = true; this.CanShutdown = true; this.CanStop = true; } static void Main(string[] args) { ServiceBase.Run(new GBLibService(args)); } protected override void Dispose(bool disposing) { base.Dispose(disposing); } protected override void OnStart(string[] args) { base.OnStart(args); this.EventLog.WriteEntry(string.Format("OnStart")); } protected override void OnStop() { base.OnStop(); this.EventLog.WriteEntry("OnStop"); } protected override void OnPause() { base.OnPause(); this.EventLog.WriteEntry("OnPause"); } protected override void OnContinue() { base.OnContinue(); this.EventLog.WriteEntry("OnContinue"); } protected override void OnShutdown() { base.OnShutdown(); this.EventLog.WriteEntry("OnShutdown"); } protected override void OnCustomCommand(int command) { base.OnCustomCommand(command); this.EventLog.WriteEntry(string.Format("OnCustomCommand: Command={0}",command)); } protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus) { this.EventLog.WriteEntry(string.Format("OnPowerEvent: powerStatus={0}", powerStatus)); return base.OnPowerEvent(powerStatus); } } }
4. Create ServiceInstaller Class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel; using System.Configuration.Install; using System.ServiceProcess; namespace ServiceApp { [RunInstaller(true)] public class MyServiceInstaller : Installer { public MyServiceInstaller () { ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller(); ServiceInstaller serviceInstaller = new ServiceInstaller(); serviceProcessInstaller.Account = ServiceAccount.LocalSystem; serviceProcessInstaller.Username = null; serviceProcessInstaller.Password = null; serviceInstaller.ServiceName = "MyService.Application"; serviceInstaller.DisplayName = "Server Windows Service"; serviceInstaller.Description = "Server Windows Service"; serviceInstaller.StartType = ServiceStartMode.Automatic; this.Installers.Add(serviceProcessInstaller); this.Installers.Add(serviceInstaller); } } }
5.Create a batch file to install/uninstall the service
@ECHO OFF SETLOCAL cls echo Press Enter to Install the service pause >NUL echo Installing Service sc create [ServiceName] start= auto DisplayName= "Service Display Name" binPath= "%CD%\Exefile.exe [/param /param /param...]" echo Done echo Press Enter to Uninstall the service pause >NUL echo Uninstalling Service sc delete [ServiceName] echo Done Pause endlocal
コメントをお書きください
bunchan747 (火曜日, 24 1月 2012 15:04)
いいね
tettsuan (水曜日, 25 1月 2012 08:05)
WCFサービスとかはWindowsサービスとして登録しておくと便利だよね。
Juicer Review (日曜日, 14 4月 2013 11:35)
This post was in fact exactly what I had been in search of!