Asp.Net Core Application Hosted in IIS - How to Pass a Startup Parameter

I have an Asp.Net Core application that can be hosted in IIS or WindowsService.

Here is the part of Program.cs

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 => { if (!string.IsNullOrWhiteSpace(certificateFilename)) { options.UseHttps(contentRoot + certificateFilename, certificatePassword); } }) .UseStartup<Startup>(); if (!string.IsNullOrWhiteSpace(environment)) { hostConfig.UseEnvironment(environment); } if (hostAsWindowsService) { hostConfig .Build() .RunAsEycService(); } else { hostConfig .UseIISIntegration() .Build() .Run(); } } } 

I cannot pass the parameter during deployment as a Windows service, so by default the project is hosted as WS. Can I pass the IIS parameter during its launch, so the correct hosting is used inside the main method (args.Contains ("- web"))?

I tried passing parameters to the Windows Service host:

1) sc.exe create MyService binPath = "c: \ Projects \ MyService.exe --ws = true"

2) by adding "--ws = true" to the "Initial Settings" in the "Windows Service" dialog box (it will not actually be saved)

+5
source share

Source: https://habr.com/ru/post/1264075/


All Articles