You should be able to use the AddCommandLine()
extension. First install the Nuget package Microsoft.Extensions.Configuration.CommandLine
and ensure you have the correct import:
using Microsoft.Extensions.Configuration;
Now update your Main
method to include the new config:
var config = new ConfigurationBuilder()
.AddJsonFile("hosting.json", optional: true) //this is not needed, but could be useful
.AddCommandLine(args)
.Build();
var builder = new WebHostBuilder()
.UseConfiguration(config) //<-- Add this
.UseStartup<Startup>()
.UseKestrel()
.UseUrls(listeningUrl);
Now you treat the command line options as configuration:
dotnet run /MySetting:SomeValue=123
And read in code:
var someValue = Configuration.GetValue<int>("MySetting:SomeValue");