Advertisement
Guest User

Untitled

a guest
Oct 1st, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. internal sealed class Program
  2. {
  3. #if DEBUG
  4. private static void Main()
  5. #else
  6. private static void NotUsedMain()
  7. #endif
  8. {
  9. XmlConfigurator.Configure();
  10. AppDomain.CurrentDomain.UnhandledException += DomainExceptionsHandler.OnDomainUnhandledException;
  11. Logger log = LogManager.GetCurrentClassLogger();
  12. using (var serviceBuilder = new ServicesBuilder())
  13. {
  14. log.Debug("Starting...");
  15. serviceBuilder.BuildApplication();
  16. serviceBuilder.OpenCommunicationServices();
  17. log.Info("MyConnectorService is running. Press <Esc> or <Enter> to stop.");
  18. while (ProcessSpecialKey(Console.ReadKey()))
  19. {
  20. }
  21. }
  22. }
  23.  
  24. private static bool ProcessSpecialKey(ConsoleKeyInfo keyInfo)
  25. {
  26. switch (keyInfo.Key)
  27. {
  28. case ConsoleKey.Enter:
  29. case ConsoleKey.Escape:
  30. return false;
  31. }
  32. return true;
  33. }
  34. }
  35. }
  36.  
  37. partial class WindowsService1 : ServiceBase
  38. {
  39. private readonly Logger _log;
  40. private readonly ServicesBuilder _serviceBuilder;
  41.  
  42. public WindowsService1()
  43. {
  44. InitializeComponent();
  45. _log = LogManager.GetCurrentClassLogger();
  46. _serviceBuilder = new ServicesBuilder();
  47. _serviceBuilder.BuildApplication();
  48. }
  49.  
  50. protected override void OnStart(string[] args)
  51. {
  52. try
  53. {
  54. _log.Info("Starting MyWindows Service...");
  55. _serviceBuilder.OpenCommunicationServices();
  56. _log.Info("MyWindows Service is running");
  57. ...
  58. }
  59.  
  60. protected override void OnStop()
  61. {
  62. try
  63. {
  64. _serviceBuilder.CloseCommunicationServices();
  65. ...
  66. }
  67.  
  68. #if !DEBUG
  69. private static void Main()
  70. #else
  71. private static void NotUsedMain()
  72. #endif
  73. {
  74. var servicesToRun = new ServiceBase[]
  75. {
  76. new WindowsService1(),
  77. };
  78. Run(servicesToRun);
  79. }
  80. }
  81. }
  82.  
  83. public class ServicesBuilder : IDisposable
  84. {
  85. public void BuildApplication()
  86. {
  87. }
  88.  
  89. public void CloseCommunicationServices()
  90. {
  91. }
  92.  
  93. public void OpenCommunicationServices()
  94. {
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement