static class Program { static MyService oService; static void Main() { if (Environment.UserInteractive == false) { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new MyService(), }; ServiceBase.Run(ServicesToRun); } else { if (ConfigurationManager.AppSettings.Count > 0) { oService = new MyService(); oService.Start(); Console.ReadLine(); oService.Stop(); } else { Console.WriteLine("Config file is missing ..."); Console.ReadLine(); } } } }And then MyService looks like this:
public partial class MyService : ServiceBase { public MyService() { this.CanStop = true; this.CanShutdown = true; this.CanPauseAndContinue = false; } protected override void OnStart(string[] args) { // startup code here //Console.WriteLine("Done With OnStart"); } protected override void OnStop() { // stop code here } protected override void OnShutdown() { // TODO: There may be a bug in the .NET Framework in that this method // is not being called on system shutdown for SYSTEM account services. // Not sure when/if it will be fixed, but I'm putting code here to stop // stuff on the off-chance that it will be called eventually. // stop code here } // These 2 methods are only used when not runnning as a Service, for testing from a Console window. public void Start() { this.OnStart(null); } public void Stop() { this.OnStop(); } }Then all you have to do is run the EXE normally and it will be like you're running the service, only it will be a Console app.