You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DI: How to register Service, that needs parsed argument as parameter #156
Hi, i want to build a cli wrapper around an existing API. The API is instantiated with a static factory method which has a parameter called environment (PROD or TEST). This environment should be an option for the cli. How do i register the service properly to use it in Commands with DI?
I think I didn't understand the question correctly.
Do you mean to register a service depending on the result of the CLI Option i.e. the PROD/TEST?
Maybe something like this is a solution?
Run
> wrapper.exe -e prod run
> Running API from PROD
Code
usingMatthiWare.CommandLine;usingMatthiWare.CommandLine.Abstractions;usingMatthiWare.CommandLine.Abstractions.Command;usingMatthiWare.CommandLine.Core.Attributes;usingMicrosoft.Extensions.DependencyInjection;usingSystem;usingSystem.Reflection;namespaceQuestion{publicenumApiEnvironment{PROD,TEST}publicclassOptions{[Name("e","environment")][Required]publicApiEnvironmentEnvironment{get;set;}}publicclassApi{privatereadonlyApiEnvironmentenvironment;privateApi(ApiEnvironmentenvironment){this.environment=environment;}publicstaticApiCreateApiFactory(ApiEnvironmentenvironment)=>new(environment);publicvoidRun(){Console.WriteLine($"Running API from {environment}");}}publicclassRunCommand:Command<Options>{publicoverridevoidOnConfigure(ICommandConfigurationBuilderbuilder){builder.Name("run").Description("Runs the API");}publicoverridevoidOnExecute(Optionsoptions){varapi=Api.CreateApiFactory(options.Environment);api.Run();}publicclassProgram{staticvoidMain(string[]args){varservices=ConfigureServices();varparser=services.BuildServiceProvider().GetRequiredService<ICommandLineParser<Options>>();parser.DiscoverCommands(Assembly.GetExecutingAssembly());parser.Parse(args);}staticIServiceCollectionConfigureServices(){varservices=newServiceCollection();services.AddCommandLineParser<Options>();returnservices;}}}}
Hey, thx for your fast reply.
Yes, you got it right, despite that my API has an interface. But when i code it like this, i have to instantiate the API in every command (there are more than 1). It would by nicer if i could register the API's interface to the services and then inject it into the command - the DI-container should be responsible for creating instances.
Do you see some way? Or is it prohibited by the parse-executeCommand-chain?
At the moment it is not possible to do this because the result of which API environment to use is only known after the service provider has already been built.
Activity
Matthiee commentedon Nov 8, 2021
Hi @SvenBM
I think I didn't understand the question correctly.
Do you mean to register a service depending on the result of the CLI Option i.e. the PROD/TEST?
Maybe something like this is a solution?
Run
Code
SvenBM commentedon Nov 8, 2021
Hey, thx for your fast reply.
Yes, you got it right, despite that my API has an interface. But when i code it like this, i have to instantiate the API in every command (there are more than 1). It would by nicer if i could register the API's interface to the services and then inject it into the command - the DI-container should be responsible for creating instances.
Do you see some way? Or is it prohibited by the parse-executeCommand-chain?
Matthiee commentedon Nov 8, 2021
At the moment it is not possible to do this because the result of which API environment to use is only known after the service provider has already been built.