locked
CommandLineConfigurationProvider RRS feed

  • Question

  • User-5485716 posted

    I am using generic host for a console app (not a daemon style service, just a shortlived app) by .AddHostedService() (I guess I can use a transient or singleton, but I don't know how to instantiate it).

    Construction Injection of  IOptions<MyConfigClass>, ILogger<MyServiceClass> etc. works fine for json provider.
    however, I would (preferably) like to use CommandLine provider for setting one of the properties of the T object from args array, but don't understand how to do this.

    Alternatively I would like to inject one string out of args directly, but don't know how to this either.

    Someone hint me?

    Monday, June 1, 2020 3:36 PM

All replies

  • User475983607 posted

    I'm not sure I understand your question.  But it is very simple to pass parameter to a console app. 

    MyConsoleApp.exe arg1 arg2 arg3...argN

    The args are available in main().

    static void Main(string[] args)
    {

    Monday, June 1, 2020 4:03 PM
  • User-5485716 posted

    Sure, but how to pass it on via DI to a service with constructor injection?
    Or better: How to let CommandLineConfigurationProvider take care of the one property that should override json settings (to IOption<T>)?

    Monday, June 1, 2020 4:06 PM
  • User475983607 posted

    Sure, but how to pass it on via DI to a service with constructor injection?
    Or better: How to let CommandLineConfigurationProvider take care of the one property that should override json settings (to IOption<T>)?

    I'm not following your question.  main() is the application entry point.  If you have another application that is invoking main() then the other application is responsible for passing the arguments.  

    Monday, June 1, 2020 4:23 PM
  • User-5485716 posted

    I have no clue why it suddenly started working exactly as expected  (using config data from args to override json),
    I have not committed enough to version control (git) for finding out what dis it, but anyway it is working:)

    Monday, June 1, 2020 6:35 PM