■ 윈도우즈 서비스를 실행하는 방법을 보여준다. (인자 전달 포함)
[Test.Common 프로젝트]
▶ ControllerConfiguration.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
using System.IO; using System.Reflection; using System.Web.Hosting; namespace Test.Common { /// <summary> /// 컨트롤러 구성 /// </summary> public class ControllerConfiguration { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서비스명 - ServiceName /// <summary> /// 서비스명 /// </summary> public string ServiceName { get; set; } #endregion #region 서비스 설명 - ServiceDescription /// <summary> /// 서비스 설명 /// </summary> public string ServiceDescription { get; set; } #endregion #region 서비스 파일 경로 - ServiceFilePath /// <summary> /// 서비스 파일 경로 /// </summary> public string ServiceFilePath { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 로드하기 - Load(sourceDirectoryPath) /// <summary> /// 로드하기 /// </summary> /// <param name="sourceDirectoryPath">소스 디렉토리 경로</param> /// <returns>구성 인스턴스</returns> public static ControllerConfiguration Load(string sourceDirectoryPath) { string filePath = Path.Combine(sourceDirectoryPath, $"{nameof(ControllerConfiguration)}.json"); string json = File.ReadAllText(filePath); ControllerConfiguration configuration = JSONHelper.GetObject<ControllerConfiguration>(json); return configuration; } #endregion #region 로드하기 - Load(assembly) /// <summary> /// 로드하기 /// </summary> /// <param name="assembly">어셈블리</param> /// <returns>구성 인스턴스</returns> public static ControllerConfiguration Load(Assembly assembly) { string currentDirectoryPath = HostingEnvironment.ApplicationPhysicalPath != null ? Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "bin") : Path.GetDirectoryName(assembly.Location); string filePath = Path.Combine(currentDirectoryPath, $"{nameof(ControllerConfiguration)}.json"); string json = File.ReadAllText(filePath); ControllerConfiguration configuration = JSONHelper.GetObject<ControllerConfiguration>(json); return configuration; } #endregion } } |
▶ ServerConfiguration.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
using System.IO; using System.Reflection; using System.Web.Hosting; namespace Test.Common { /// <summary> /// 서버 구성 /// </summary> public class ServerConfiguration { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서비스명 - ServiceName /// <summary> /// 서비스명 /// </summary> public string ServiceName { get; set; } #endregion #region 서비스 설명 - ServiceDescription /// <summary> /// 서비스 설명 /// </summary> public string ServiceDescription { get; set; } #endregion #region 서비스 파일 경로 - ServiceFilePath /// <summary> /// 서비스 파일 경로 /// </summary> public string ServiceFilePath { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 로드하기 - Load(sourceDirectoryPath) /// <summary> /// 로드하기 /// </summary> /// <param name="sourceDirectoryPath">소스 디렉토리 경로</param> /// <returns>구성 인스턴스</returns> public static ServerConfiguration Load(string sourceDirectoryPath) { string filePath = Path.Combine(sourceDirectoryPath, $"{nameof(ServerConfiguration)}.json"); string json = File.ReadAllText(filePath); ServerConfiguration configuration = JSONHelper.GetObject<ServerConfiguration>(json); return configuration; } #endregion #region 로드하기 - Load(assembly) /// <summary> /// 로드하기 /// </summary> /// <param name="assembly">어셈블리</param> /// <returns>구성 인스턴스</returns> public static ServerConfiguration Load(Assembly assembly) { string currentDirectoryPath = HostingEnvironment.ApplicationPhysicalPath != null ? Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "bin") : Path.GetDirectoryName(assembly.Location); string filePath = Path.Combine(currentDirectoryPath, $"{nameof(ServerConfiguration)}.json"); string json = File.ReadAllText(filePath); ServerConfiguration configuration = JSONHelper.GetObject<ServerConfiguration>(json); return configuration; } #endregion } } |
▶ ServiceStatus.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
namespace Test.Common { /// <summary> /// 서비스 상태 /// </summary> public class ServiceStatus { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 서비스 설치 여부 - IsServiceInstalled /// <summary> /// 서비스 설치 여부 /// </summary> public bool IsServiceInstalled { get; set; } #endregion #region 서비스 실행 여부 - IsServerRunning /// <summary> /// 서비스 실행 여부 /// </summary> public bool IsServiceRunning { get; set; } #endregion } } |
▶ ServiceHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
using System.Configuration.Install; using System.Collections; using System.ServiceProcess; namespace Test.Common { /// <summary> /// 서비스 헬퍼 /// </summary> public static class ServiceHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 서비스 상태 구하기 - GetServiceStatus(serviceName) /// <summary> /// 서비스 상태 구하기 /// </summary> /// <param name="serviceName">서비스명</param> /// <returns>서비스 상태</returns> public static ServiceStatus GetServiceStatus(string serviceName) { ServiceStatus status = new ServiceStatus(); ServiceController[] serviceControllerArray = ServiceController.GetServices(); foreach(ServiceController serviceController in serviceControllerArray) { if(serviceController.ServiceName == serviceName) { status.IsServiceInstalled = true; if(serviceController.Status == ServiceControllerStatus.Running) { status.IsServiceRunning = true; } break; } } return status; } #endregion #region 서비스 설치하기 - InstallService(serviceFilePath) /// <summary> /// 서비스 설치하기 /// </summary> /// <param name="serviceFilePath">서비스 파일 경로</param> public static void InstallService(string serviceFilePath) { IDictionary dictionary = new Hashtable(); AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFilePath, null); assemblyInstaller.UseNewContext = true; assemblyInstaller.Install(dictionary); assemblyInstaller.Commit(dictionary); } #endregion #region 서비스 설치 취소하기 - UninstallService(serviceFilePath) /// <summary> /// 서비스 설치 취소하기 /// </summary> /// <param name="serviceFilePath">서비스 파일 경로</param> public static void UninstallService(string serviceFilePath) { IDictionary dictionary = new Hashtable(); AssemblyInstaller assemblyInstaller = new AssemblyInstaller(serviceFilePath, null); assemblyInstaller.UseNewContext = true; assemblyInstaller.Uninstall(dictionary); } #endregion #region 서비스 시작하기 - StartService(serviceName, argumentArray) /// <summary> /// 서비스 시작하기 /// </summary> /// <param name="serviceName">서비스명</param> /// <param name="argumentArray">인자 배열</param> public static void StartService(string serviceName, string[] argumentArray) { using(ServiceController serviceController = new ServiceController(serviceName)) { if(serviceController.Status == ServiceControllerStatus.Stopped) { serviceController.Start(argumentArray); } } } #endregion #region 서비스 중단하기 - StopService(serviceName) /// <summary> /// 서비스 중단하기 /// </summary> /// <param name="serviceName">서비스명</param> public static void StopService(string serviceName) { using(ServiceController serviceController = new ServiceController(serviceName)) { if(serviceController.Status == ServiceControllerStatus.Running) { serviceController.Stop(); } } } #endregion } } |
[Test.Server.WindowsService 프로젝트]
▶ ServerConfiguration.json
1 2 3 4 5 6 7 |
{ "ServiceName" : "Test_Server_WindowsService", "ServiceDescription" : "Test Server WindowsService 입니다.", "ServiceFilePath" : "Test.Server.WindowsService.exe" } |
▶ ProjectInstaller.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
using System.ComponentModel; using System.Configuration.Install; using System.Reflection; using System.ServiceProcess; using Test.Common; namespace Test.Server.WindowsService { /// <summary> /// 프로젝트 설치자 /// </summary> [RunInstaller(true)] public partial class ProjectInstaller : Installer { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 구성 /// </summary> private ServerConfiguration configuration; /// <summary> /// 서비스 프로세스 설치자 /// </summary> private ServiceProcessInstaller serviceProcessInstaller; /// <summary> /// 서비스 설치자 /// </summary> private ServiceInstaller serviceInstaller; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ProjectInstaller() /// <summary> /// 생성자 /// </summary> public ProjectInstaller() { this.configuration = ServerConfiguration.Load(Assembly.GetExecutingAssembly()); this.serviceProcessInstaller = new ServiceProcessInstaller(); serviceProcessInstaller.Account = ServiceAccount.LocalSystem; serviceProcessInstaller.Password = null; serviceProcessInstaller.Username = null; this.serviceInstaller = new ServiceInstaller(); serviceInstaller.Description = this.configuration.ServiceDescription; serviceInstaller.DisplayName = this.configuration.ServiceName; serviceInstaller.ServiceName = this.configuration.ServiceName; serviceInstaller.StartType = ServiceStartMode.Automatic; Installers.AddRange(new Installer[] { this.serviceProcessInstaller, this.serviceInstaller }); } #endregion } } |
▶ ServerService.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
using System; using System.ServiceProcess; using System.Reflection; using System.Threading; using Test.Common; namespace Test.Server.WindowsService { /// <summary> /// 서버 서비스 /// </summary> public partial class ServerService : ServiceBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 구성 /// </summary> private ServerConfiguration configuration = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ServerService() /// <summary> /// 생성자 /// </summary> public ServerService() { ThreadPool.SetMinThreads(500, 500); ThreadPool.SetMaxThreads(5000, 5000); this.configuration = ServerConfiguration.Load(Assembly.GetExecutingAssembly()); AutoLog = false; ServiceName = this.configuration.ServiceName; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 테스트 하기 - Test(argumentArray) /// <summary> /// 테스트 하기 /// </summary> /// <param name="argumentArray">인자 배열</param> public void Test(string[] argumentArray) { OnStart(argumentArray); Console.ReadLine(); OnStop(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 시작시 처리하기 - OnStart(argumentArray) /// <summary> /// 시작시 처리하기 /// </summary> /// <param name="argumentArray">인자 배열</param> protected override void OnStart(string[] argumentArray) { if(argumentArray != null) { File.WriteAllText("d:\\test.log", string.Empty); File.WriteAllLines("d:\\test.log", argumentArray); } } #endregion #region 중단시 처리하기 - OnStop() /// <summary> /// 중단시 처리하기 /// </summary> protected override void OnStop() { } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; using System.ServiceProcess; namespace Test.Server.WindowsService { /// <summary> /// 프로그램 /// </summary> static class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main(argumentArray) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentArray">인자 배열</param> private static void Main(string[] argumentArray) { if(Environment.UserInteractive) { ServerService service = new ServerService(); service.Test(argumentArray); } else { ServiceBase[] serviceBaseArray; serviceBaseArray = new ServiceBase[] { new ServerService() }; ServiceBase.Run(serviceBaseArray); } } #endregion } } |
[Test.Controller.WindowsService 프로젝트]
▶ ControllerConfiguration.json
1 2 3 4 5 6 7 |
{ "ServiceName" : "Test_Controller_WindowsService", "ServiceDescription" : "Test Controller WindowsService 입니다.", "ServiceFilePath" : "Test.Controller.WindowsService.exe" } |
▶ ProjectInstaller.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
using System.ComponentModel; using System.Configuration.Install; using System.Reflection; using System.ServiceProcess; using Test.Common; namespace Test.Controller.WindowsService { /// <summary> /// 프로젝트 설치자 /// </summary> [RunInstaller(true)] public partial class ProjectInstaller : Installer { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 구성 /// </summary> private ControllerConfiguration configuration; /// <summary> /// 서비스 프로세스 설치자 /// </summary> private ServiceProcessInstaller serviceProcessInstaller; /// <summary> /// 서비스 설치자 /// </summary> private ServiceInstaller serviceInstaller; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ProjectInstaller() /// <summary> /// 생성자 /// </summary> public ProjectInstaller() { this.configuration = ControllerConfiguration.Load(Assembly.GetExecutingAssembly()); this.serviceProcessInstaller = new ServiceProcessInstaller(); serviceProcessInstaller.Account = ServiceAccount.LocalSystem; serviceProcessInstaller.Password = null; serviceProcessInstaller.Username = null; this.serviceInstaller = new ServiceInstaller(); serviceInstaller.Description = this.configuration.ServiceDescription; serviceInstaller.DisplayName = this.configuration.ServiceName; serviceInstaller.ServiceName = this.configuration.ServiceName; serviceInstaller.StartType = ServiceStartMode.Automatic; Installers.AddRange(new Installer[] { this.serviceProcessInstaller, this.serviceInstaller }); } #endregion } } |
▶ ControllerService.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
using System; using System.Reflection; using System.ServiceProcess; using System.Threading; using Test.Common; namespace Test.Controller.WindowsService { /// <summary> /// 컨트롤러 서비스 /// </summary> public partial class ControllerService : ServiceBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 구성 /// </summary> private ControllerConfiguration configuration = null; /// <summary> /// 서비스명 /// </summary> private const string SERVICE_NAME = "Test_Server_WindowsService"; /// <summary> /// 서비스 파일 경로 /// </summary> private const string SERVICE_FILE_PATH = @"d:\TestSolution\Bin\Release\Test.Server\Test.Server.WindowsService.exe"; /// <summary> /// 서비스 인자 배열 /// </summary> private readonly string[] SERVICE_ARGUMENT_ARRAY = new string[] { "TEST1", "TEST2", "TEST3" }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ControllerService() /// <summary> /// 생성자 /// </summary> public ControllerService() { ThreadPool.SetMinThreads(500, 500); ThreadPool.SetMaxThreads(5000, 5000); this.configuration = ControllerConfiguration.Load(Assembly.GetExecutingAssembly()); AutoLog = false; ServiceName = this.configuration.ServiceName; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 테스트 하기 - Test(argumentArray) /// <summary> /// 테스트 하기 /// </summary> /// <param name="argumentArray">인자 배열</param> public void Test(string[] argumentArray) { OnStart(argumentArray); Console.ReadLine(); OnStop(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 시작시 처리하기 - OnStart(argumentArray) /// <summary> /// 시작시 처리하기 /// </summary> /// <param name="argumentArray">인자 배열</param> protected override void OnStart(string[] argumentArray) { ServiceStatus status = ServiceHelper.GetServiceStatus(SERVICE_NAME); if(status.IsServiceInstalled) { if(!status.IsServiceRunning) { ServiceHelper.StartService(SERVICE_NAME, SERVICE_ARGUMENT_ARRAY); } } else { ServiceHelper.InstallService(SERVICE_FILE_PATH); ServiceHelper.StartService(SERVICE_NAME, SERVICE_ARGUMENT_ARRAY); } } #endregion #region 중단시 처리하기 - OnStop() /// <summary> /// 중단시 처리하기 /// </summary> protected override void OnStop() { ServiceStatus status = ServiceHelper.GetServiceStatus(SERVICE_NAME); if(status.IsServiceInstalled) { if(status.IsServiceRunning) { ServiceHelper.StopService(SERVICE_NAME); ServiceHelper.UninstallService(SERVICE_FILE_PATH); } else { ServiceHelper. UninstallService (SERVICE_NAME); } } } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
using System; using System.ServiceProcess; namespace Test.Controller.WindowsService { /// <summary> /// 프로그램 /// </summary> static class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main(argumentArray) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentArray">인자 배열</param> private static void Main(string[] argumentArray) { if(Environment.UserInteractive) { ControllerService service = new ControllerService(); service.Test(argumentArray); } else { ServiceBase[] serviceBaseArray; serviceBaseArray = new ServiceBase[] { new ControllerService() }; ServiceBase.Run(serviceBaseArray); } } #endregion } } |
[Test.Controller.Monitor 프로젝트]
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
using System; using System.Collections; using System.Configuration.Install; using System.Reflection; using System.ServiceProcess; using System.Windows.Forms; using Test.Common; namespace Test.Controller.Monitor { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 구성 /// </summary> private ControllerConfiguration configuration = null; /// <summary> /// 서비스 설치 여부 /// </summary> private bool isServiceInstalled = false; /// <summary> /// 서비스 실행 여부 /// </summary> private bool isServiceRunning = false; /// <summary> /// 타이머 /// </summary> private Timer timer = null; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); #region 타이머를 설정한다. this.timer = new Timer(); this.timer.Interval = 1000; #endregion #region 구성 파일을 로드한다. this.configuration = ControllerConfiguration.Load(Assembly.GetExecutingAssembly()); #endregion #region 서비스 상태를 설정하고 표시한다. SetServiceStatus(); DisplayServiceStatus(); #endregion #region 서비스 버튼 이용 가능 여부를 설정한다. SetControlEnabled(); #endregion this.installButton.Click += installButton_Click; this.uninstallButton.Click += uninstallButton_Click; this.startButton.Click += startButton_Click; this.stopButton.Click += stopButton_Click; this.topMostCheckBox.CheckedChanged += topMostCheckBox_CheckedChanged; this.timer.Tick += timer_Tick; this.timer.Start(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 설치 버튼 클릭시 처리하기 - installButton_Click(sender, e) /// <summary> /// 설치 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void installButton_Click(object sender, EventArgs e) { try { this.serviceGroupBox.Enabled = false; InstallService(); } finally { this.serviceGroupBox.Enabled = true; } } #endregion #region 설치 제거 버튼 클릭시 처리하기 - uninstallButton_Click(sender, e) /// <summary> /// 설치 제거 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void uninstallButton_Click(object sender, EventArgs e) { try { this.serviceGroupBox.Enabled = false; UninstallService(); } finally { this.serviceGroupBox.Enabled = true; } } #endregion #region 시작 버튼 클릭시 처리하기 - startButton_Click(sender, e) /// <summary> /// 시작 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void startButton_Click(object sender, EventArgs e) { try { this.serviceGroupBox.Enabled = false; StartService(); } finally { this.serviceGroupBox.Enabled = true; } } #endregion #region 중단 버튼 클릭시 처리하기 - stopButton_Click(sender, e) /// <summary> /// 중단 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stopButton_Click(object sender, EventArgs e) { try { this.serviceGroupBox.Enabled = false; StopService(); } finally { this.serviceGroupBox.Enabled = true; } } #endregion #region 최상위 체크 박스 체크 변경시 처리하기 - topMostCheckBox_CheckedChanged(sender, e) /// <summary> /// 최상위 체크 박스 체크 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void topMostCheckBox_CheckedChanged(object sender, EventArgs e) { TopMost = this.topMostCheckBox.Checked; } #endregion #region 타이머 틱 발생시 처리하기 - timer_Tick(sender, e) /// <summary> /// 타이머 틱 발생시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void timer_Tick(object sender, EventArgs e) { SetServiceStatus(); DisplayServiceStatus(); SetControlEnabled(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 서비스 상태 표시하기 - DisplayServiceStatus() /// <summary> /// 서비스 상태 표시하기 /// </summary> private void DisplayServiceStatus() { if(this.isServiceInstalled) { if(this.isServiceRunning) { this.serviceStatusTextBox.Text = "RUNNING"; } else { this.serviceStatusTextBox.Text = "STOP"; } } else { this.serviceStatusTextBox.Text = "NOT INSTALLED"; } } #endregion #region 서비스 상태 설정하기 - SetServiceStatus() /// <summary> /// 서비스 상태 설정하기 /// </summary> private void SetServiceStatus() { this.isServiceInstalled = false; this.isServiceRunning = false; ServiceController[] serviceControllerArray = ServiceController.GetServices(); foreach(ServiceController serviceController in serviceControllerArray) { if(serviceController.ServiceName == this.configuration.ServiceName) { this.isServiceInstalled = true; if(serviceController.Status == ServiceControllerStatus.Running) { this.isServiceRunning = true; } break; } } } #endregion #region 컨트롤 사용 가능 여부 설정하기 - SetControlEnabled() /// <summary> /// 컨트롤 사용 가능 여부 설정하기 /// </summary> private void SetControlEnabled() { if(this.isServiceInstalled) { this.installButton.Enabled = false; this.uninstallButton.Enabled = true; if(this.isServiceRunning) { this.startButton.Enabled = false; this.stopButton.Enabled = true; } else { this.startButton.Enabled = true; this.stopButton.Enabled = false; } } else { this.installButton.Enabled = true; this.uninstallButton.Enabled = false; this.startButton.Enabled = false; this.stopButton.Enabled = false; } } #endregion #region 서비스 설치하기 - InstallService() /// <summary> /// 서비스 설치하기 /// </summary> private void InstallService() { IDictionary dictionary = new Hashtable(); try { AssemblyInstaller assemblyInstaller = new AssemblyInstaller(this.configuration.ServiceFilePath, null); assemblyInstaller.UseNewContext = true; assemblyInstaller.Install(dictionary); assemblyInstaller.Commit(dictionary); MessageBox.Show("서비스 설치가 완료되었습니다."); } catch(Exception exception) { MessageBox.Show("서비스 설치를 실패했습니다." + Environment.NewLine + exception.Message); } } #endregion #region 서비스 삭제하기 - UninstallService() /// <summary> /// 서비스 삭제하기 /// </summary> private void UninstallService() { IDictionary dictionary = new Hashtable(); try { AssemblyInstaller assemblyInstaller = new AssemblyInstaller(this.configuration.ServiceFilePath, null); assemblyInstaller.UseNewContext = true; assemblyInstaller.Uninstall(dictionary); MessageBox.Show("서비스 설치 제거를 완료했습니다."); } catch(Exception exception) { MessageBox.Show("서비스 설치 제거를 실패했습니다." + Environment.NewLine + exception.Message); } } #endregion #region 서비스 시작하기 - StartService() /// <summary> /// 서비스 시작하기 /// </summary> private void StartService() { try { using(ServiceController serviceController = new ServiceController(this.configuration.ServiceName)) { if(serviceController.Status == ServiceControllerStatus.Stopped) { serviceController.Start(); } } MessageBox.Show("서비스가 시작되었습니다."); } catch(Exception exception) { MessageBox.Show("서비스 시작을 실패했습니다." + Environment.NewLine + exception.Message); } } #endregion #region 서비스 중단하기 - StopService() /// <summary> /// 서비스 중단하기 /// </summary> private void StopService() { try { using(ServiceController serviceController = new ServiceController(this.configuration.ServiceName)) { if(serviceController.Status == ServiceControllerStatus.Running) { serviceController.Stop(); } } MessageBox.Show("서비스가 중단되었습니다."); } catch(Exception exception) { MessageBox.Show("서비스 중단을 실패했습니다." + Environment.NewLine + exception.Message); } } #endregion } } |
※ Test.Controller.Monitor.exe 프로그램은 윈도우즈 10에서 관리자 권한으로 실행해야 한다.