title: Does a .NET Windows service require a call to ServiceBase.Run()
body: <p>I'm fairly new at working with Windows services but I found a peculiar incident and I would like some clarification.  I have a Windows service written in C# which I install and start using the command line (great instructions found on stackoverflow).  The  main method of my service looks like this:</p>
  
  <pre><code>    static void Main(string[] args)
      {
          if (args.Length == 0)
          {
              ServiceBase.Run(new MyServiceName());
          }
          else if (args.Length == 1)
          {
              const string name = "MyServiceName";
              Type type = typeof(MyAssembly);
              switch (args[0])
              {
                  case "-install":
                      ServiceUtils.InstallService(name, type);
                      ServiceUtils.StartService(args, name);
                      break;
                  case "-uninstall":
                      ServiceUtils.StopService(name);
                      ServiceUtils.UninstallService(name, type);
                      break;
                  default:
                      throw new NotImplementedException();
              }
          }
      }
  </code></pre>
  
  <p>When I debug, I ALWAYS send one parameter (-install) to the application.  Because of this, the first if statement <code>(if (args.Length == 0)</code> is NEVER executed.  This is expected and my service is installed and started just fine.  However, if I remove that if statement and just leave the <code>if (args.Length == 1)</code> statement, my service installs correctly but it does not start and I get the following error:</p>
  
  <blockquote>
    <p>Cannot start MyServiceName on computer '.'</p>
  </blockquote>
  
  <p>My question is: <strong>Why is the code in the first if statement needed when it is NEVER executed in my application?</strong></p>
  
  <p>Here is the supporting code for the InstallService and StartService methods (which I got from stackoverflow also):</p>
  
  <pre><code>    public static void InstallService(string serviceName, Type t)
      {
          if (IsInstalled(serviceName)) return;
  
          try
          {
              Assembly a = t.Assembly;
              using (AssemblyInstaller installer = GetInstaller(a))
              {
                  IDictionary state = new Hashtable();
                  try
                  {
                      installer.Install(state);
                      installer.Commit(state);
                  }
                  catch
                  {
                      try
                      {
                          installer.Rollback(state);
                      }
                      catch
                      { }
                      throw;
                  }
              }
          }
          catch
          {
              throw;
          }
      }
  
      public static void StartService(string[] args, string serviceName)
      {
          if (!IsInstalled(serviceName)) return;
  
          Console.WriteLine("Service is installed.  Attempting to start service.");
  
          ServiceController sc = new ServiceController();
          sc.ServiceName = serviceName;
  
          if (sc.Status == ServiceControllerStatus.Stopped)
          {
              Console.WriteLine("Starting {0}: ", sc.ServiceName);
              try
              {
                  sc.Start(args);
                  sc.WaitForStatus(ServiceControllerStatus.Running);
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
              }
          }
      }
  </code></pre>