Advertisement
Guest User

ServiceBase Extension for hosting service in a Console app

a guest
Apr 25th, 2013
1,314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 KB | None | 0 0
  1. // ServiceBaseExtensions.cs
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using System.ServiceProcess;
  7.  
  8. namespace MyNamespace
  9. {
  10.     public static class ServiceBaseExtensions
  11.     {
  12.         /// <summary>
  13.         /// Runs a given collection of services and hosts them in either
  14.         /// Console Application or Windows Service depending on
  15.         /// how the application is executed (in interactive mode or not).
  16.         ///
  17.         /// The output type of the application must be set to Console:
  18.         /// Project properties -> Output type: Console Application.
  19.         /// </summary>
  20.         /// <param name="servicesToRun">Array of services to run.</param>
  21.         /// <param name="args">Arguments to be passed to the services.</param>
  22.         public static void Run(this ServiceBase[] servicesToRun, string[] args)
  23.         {
  24.             if (!Environment.UserInteractive)
  25.             {
  26.                 ServiceBase.Run(servicesToRun);
  27.                 return;
  28.             }
  29.  
  30.             Console.WriteLine("Running services in interactive mode...");
  31.             Console.WriteLine();
  32.  
  33.             CallServiceBaseMethod(servicesToRun, "OnStart", new object[] { args }, "Starting");
  34.            
  35.             Console.WriteLine("Press any key to stop the services...");
  36.             Console.ReadKey();
  37.  
  38.             CallServiceBaseMethod(servicesToRun, "OnStop", null, "Stopping");
  39.  
  40.             Console.WriteLine();
  41.             Console.WriteLine("Press any key to exit... ");
  42.             Console.ReadKey();
  43.         }
  44.  
  45.         private static void CallServiceBaseMethod(IEnumerable<ServiceBase> services,
  46.             string methodName, object[] methodArgs, string consoleMessage)
  47.         {
  48.             MethodInfo onStopMethod = typeof(ServiceBase).GetMethod(methodName,
  49.                 BindingFlags.Instance | BindingFlags.NonPublic);
  50.             foreach (ServiceBase service in services)
  51.             {
  52.                 Console.Write("{0} '{1}' ... ", consoleMessage, service.ServiceName);
  53.                 onStopMethod.Invoke(service, methodArgs);
  54.                 Console.Write("OK");
  55.             }
  56.             Console.WriteLine();
  57.         }
  58.     }
  59. }
  60.  
  61.  
  62. // Program.cs
  63.  
  64. /*
  65. SAMPLE USAGE:
  66. */
  67.  
  68. namespace MyNamespace
  69. {
  70.     static class Program
  71.     {
  72.         static void Main(string[] args)
  73.         {
  74.             System.ServiceProcess.ServiceBase[] services =
  75.                 new System.ServiceProcess.ServiceBase[] { new MyWindowsService() };
  76.             services.Run(args);
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement