code_junkie

Easier way to start debugging a windows service in C#

Nov 11th, 2011
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. [Conditional("DEBUG_SERVICE")]
  2. private static void DebugMode()
  3. {
  4. Debugger.Break();
  5. }
  6.  
  7. public override void OnStart()
  8. {
  9. DebugMode();
  10. /* ... do the rest */
  11. }
  12.  
  13. public static int Main(string[] args)
  14. {
  15. if (!Environment.UserInteractive)
  16. {
  17. // Startup as service.
  18. }
  19. else
  20. {
  21. // Startup as application
  22. }
  23. }
  24.  
  25. /ConsoleRunner
  26. /....
  27. /ServiceRunner
  28. /....
  29. /ApplicationLogic
  30. /....
  31.  
  32. using System;
  33. using System.Threading;
  34. using System.Configuration;
  35.  
  36. public class ServiceEventHandler
  37. {
  38. Timer _timer;
  39. public ServiceEventHandler()
  40. {
  41. // get configuration etc.
  42. _timer = new Timer(
  43. new TimerCallback(EventTimerCallback)
  44. , null
  45. , Timeout.Infinite
  46. , Timeout.Infinite);
  47. }
  48.  
  49. private void EventTimerCallback(object state)
  50. {
  51. // do something
  52. }
  53.  
  54. public void StartEventLoop()
  55. {
  56. // wait a minute, then run every 30 minutes
  57. _timer.Change(TimeSpan.Parse("00:01:00"), TimeSpan.Parse("00:30:00");
  58. }
  59. }
  60.  
  61. #if DEBUG
  62. if (!System.Diagnostics.Debugger.IsAttached)
  63. {
  64. System.Diagnostics.Debugger.Break();
  65. }
  66. #endif
  67.  
  68. static void Main()
  69. {
  70. #if DEBUG
  71. // Run as interactive exe in debug mode to allow easy
  72. // debugging.
  73.  
  74. var service = new MyService();
  75. service.OnStart(null);
  76.  
  77. // Sleep the main thread indefinitely while the service code
  78. // runs in .OnStart
  79.  
  80. Thread.Sleep(Timeout.Infinite);
  81. #else
  82. // Run normally as service in release mode.
  83.  
  84. ServiceBase[] ServicesToRun;
  85. ServicesToRun = new ServiceBase[]{ new MyService() };
  86. ServiceBase.Run(ServicesToRun);
  87. #endif
  88. }
  89.  
  90. static void Main(string[] args) {
  91. if (Console.In != StreamReader.Null) {
  92. if (args.Length > 0 && args[0] == "/console") {
  93. // Start your service work.
  94. }
  95. }
  96. }
  97.  
  98. protected override void OnStart(string[] args)
  99. {
  100. if (args.Contains<string>("DEBUG_SERVICE"))
  101. {
  102. Debugger.Break();
  103. }
  104. ...
  105. }
  106.  
  107. protected override void OnStart(string[] args)
  108. {
  109. if (args.Contains<string>("DEBUG_SERVICE"))
  110. {
  111. Debugger.Break();
  112. }
  113. ...
  114. }
  115.  
  116. protected override void OnStart(string[] args)
  117. {
  118. if (args.Contains<string>("DEBUG_SERVICE"))
  119. {
  120. Debugger.Break();
  121. }
  122. ...
  123. }
  124.  
  125. #if DEBUG
  126. System.Diagnostics.Debugger.Break();
  127. #endif
  128.  
  129. /// <summary>
  130. /// The main entry point for the application.
  131. /// </summary>
  132. static void Main()
  133. {
  134. ServiceBase[] ServicesToRun;
  135. ServicesToRun = new ServiceBase[]
  136. {
  137. new MyService(),
  138. new MyServiceDebug()
  139. };
  140. ServiceBase.Run(ServicesToRun);
  141. }
Add Comment
Please, Sign In to add comment