title: How to make windows service application so it can run as a standalone program as well?
body: <p>I'll start with an example: Apache web server (under Windows) has a nice feature: it can be both run as a standalone application (with current users privileges), and that it can be installed and run as a windows service directly (as local system account), using same executable.</p>
  
  <p>In order for application to be run as a standalone app, all it needs to do is along the lines of having static public Main() in some public class.</p>
  
  <p>In order for application to be installable and runnable as service, it has to implement ServiceBase and Installer classes in certain way. But, if application like this is run as standalone app, it will show message box.</p>
  
  <p>How can this Apache-like mode of operation be achieved? I believe solution is simple, but I don't really have an idea where to start.</p>
  
  <p>Piece of code that follows is used to invoke service. Can it be modified to allow standalone usage?</p>
  
  <pre><code>static class Program
  {
      /// &lt;summary&gt;
      /// The main entry point for the application.
      /// &lt;/summary&gt;
      static void Main()
      {
          ServiceBase[] ServicesToRun;
          ServicesToRun = new ServiceBase[] 
          { 
              new Service() // defined elsewhere as Service : ServiceBase
          };
          ServiceBase.Run(ServicesToRun);
      }
  }
  </code></pre>
  
  <p>My language of choice is C#.</p>
  
  <p><strong>Edit:</strong> Currently, I have abstracted common code into separate assembly (let's call it Library.dll), and I have two executables: Console.exe and Service.exe, which are standalone and windows service applications, respectively, and both are just means to invoking  Library.dll.</p>
  
  <p>My goal is to merge those two executables into one, that will still call to Library.dll.</p>