Repository navigation
Expand file tree
/
Copy pathWindowsService.cs
More file actions
70 lines (61 loc) · 1.78 KB
/
Copy pathWindowsService.cs
File metadata and controls
70 lines (61 loc) · 1.78 KB
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 log4net;
using System;
using System.ServiceProcess;
namespace WinSvcExecutor
{
/// <summary>
/// Windows service. Redefines default service behaviour.
/// </summary>
public partial class WindowsService<TAppService, TOptions> : ServiceBase
where TAppService : AppServiceBase<TOptions>
where TOptions : OptionsBase
{
/// <summary>
/// Logger
/// </summary>
private ILog Logger = LogManager.GetLogger(string.Format("WindowsService-{0}", typeof(TAppService).Name));
/// <summary>
/// App service
/// </summary>
private TAppService _appService;
/// <summary>
/// Constructor with appService
/// </summary>
/// <param name="appService"></param>
public WindowsService(TAppService appService) : base()
{
_appService = appService;
}
/// <summary>
/// Service start
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
// try to start service
bool status = false;
try
{
status = _appService.Start();
}
catch (Exception e)
{
Logger.ErrorFormat("Failed to start with Exception {0}: {1}.", e.GetType().Name, e.Message);
}
// stop service
if (!status)
{
Logger.ErrorFormat("Failed to start. Will clean up and stop.");
_appService.CleanUp();
Stop();
}
}
/// <summary>
/// Service stop
/// </summary>
protected override void OnStop()
{
_appService.Stop();
}
}
}