title: Asp.Net Core app hosted in IIS - how to pass startup parameter
body: <p>I have a Asp.Net Core application wich can be hosted in IIS or as a WindowsService.</p>
  
  <p>Here is part of a Program.cs</p>
  
  <pre><code>public static class Program
  {
      public static void Main(string[] args)
      {
          var hostAsWindowsService = !args.Contains("--web");
  
          var contentRoot = hostAsWindowsService ? Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) : Directory.GetCurrentDirectory();
  
          var config = new ConfigurationBuilder()
              .SetBasePath(contentRoot)
              .AddJsonFile("host.json")
              .AddCommandLine(args)
              .AddEnvironmentVariables()
              .Build();
  
          var environment = config["environment"];
          var urls = config["server.urls"];
          var certificateFilename = config["certificate.filename"];
          var certificatePassword = config["certificate.password"];
  
          var hostConfig = new WebHostBuilder()
              .UseConfiguration(config)
              .UseUrls(urls)
              .UseContentRoot(contentRoot)
              .UseKestrel(options =&gt;
                          {
                              if (!string.IsNullOrWhiteSpace(certificateFilename))
                              {
  
                                  options.UseHttps(contentRoot + certificateFilename, certificatePassword);
                              }
                          })
              .UseStartup&lt;Startup&gt;();
  
          if (!string.IsNullOrWhiteSpace(environment))
          {
              hostConfig.UseEnvironment(environment);
          }
  
          if (hostAsWindowsService)
          {
              hostConfig
                  .Build()
                  .RunAsEycService();
          }
          else
          {
              hostConfig
                  .UseIISIntegration()
                  .Build()
                  .Run();
          }
      }
  }
  </code></pre>
  
  <p>I can not pass a parameter while hosted as a Windows Service so by default the project is hosted as WS. Can I actually pass a parameter to IIS while it starts my Site so the proper hosting inside Main method is used (args.Contains("--web"))?</p>
  
  <p>I tried to pass parameters to Windows Service host:</p>
  
  <p>1) sc.exe create MyService binPath= "c:\Projects\MyService.exe --ws=true"</p>
  
  <p>2) by adding "--ws=true" to "Start parameters" in Windows Service dialog (it actually won't save)</p>