-
Notifications
You must be signed in to change notification settings - Fork 395
Question: Usage models with Service Collection (not using Generic Host) #1455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I've been distracted from looking at the Hosting package lately while we work on stabilizing the core library, but playing nicely with DI is definitely an important goal. @fredrikhr Do we have a good sample for this, potentially including the proposed API changes in the open PRs (e.g. #1356)? I think a how-to document would be very helpful in assessing the overall shape of the API and the proposed changes. |
@jonsequitur @fredrikhr thanks for the speedy reply! Per the item linked, I'm specifically looking at not using any Im not using hosted services and am not using configuration, or and logical However if you do have any examples that just work with a built service collection in perhaps a similar way to this, that would be great... |
You can do something like the following and delegate the call to your var commandLineBuilder =
new CommandLineBuilder(rootCommand)
.UseDefaults()
.UseMiddleware(
context =>
{
context.BindingContext.AddService(
typeof(MyService),
theSystemCommandLineServiceProvider => yourServiceProvider.GetService(typeof(MyService)));
}); This is a deliberately limited shim to provide your own services when binding and shouldn't be seen as a way to replace the System.CommandLine implementations (e.g. |
@jonsequitur thanks... So I can then "inject" that service into the From a lifecycle perspective, I create, configure and build the service collection up front, and and I've not looked at coding anything as yet, just looking at feasibility. I'd suggest something a bit more generic that perhaps reflects the services from the service collection dynamically based on the command handlers constructor, might be a consideration of a future feature. However any approach there may need to be configurable as your currently suggested approach is obviously DI container agnostic. I'd only otherwise suggest that having a Generic function that would say allow you to do this (sorta) might be better syntactic sugar...to remove the typeof's: ar commandLineBuilder =
new CommandLineBuilder(rootCommand)
.UseDefaults()
.UseMiddleware(
context =>
{
context.BindingContext.AddService<MyService>(
theSystemCommandLineServiceProvider => yourServiceProvider.GetRequiredService<MyService>();
}); however I'm slightly lost as to the difference between |
You can inject into the handler method. Injection into command handlers isn't supported by default but it's not hard to do. #1356 has an example using the generic host but that's not required. Generally though we've leaned toward services and arguments being bound to a handler single method, which also happens to align to the minimal web API design.
We're heading in this direction with our source generator support: https://github.com/dotnet/command-line-api/tree/main/src/System.CommandLine.Generator.
There is a generically-typed overload. |
@jonsequitur thanks for the help, I think I can probably make things work! TBH I'd rather re-structure code not to integrate services into the CLI commands but the other way around, and construct the needed service container on an as needed basis dependent on the command's needs. However when is the package destined for a non pre-release version ? |
Actually, the docs need a lot of work... Define the difference and capabilities for Command, Options, Arguments etc. would be a start... Digging around the open/closed issues for examples based on others questions is is not really helpful. |
Did you happen on this and is it helpful at all? https://github.com/dotnet/command-line-api/blob/main/docs/Syntax-Concepts-and-Parser.md |
Helpful of course, but not an exhaustively complete API guide, that covers things like |
I have been using a different cli library, that has some great features, and is very usable, but lacks functionality such as tab completion (per
dotnet
suggest`) However it does work well with using a raw ServiceCollection and provides the ability to inject those services into commands.Im assessing the lift to migrate.
I see that
command-line-api
has Host Builder support (via.UseHost
by scanning issues - open and closed) bit not specific documentation.Are there any examples out there of just using the ServiceCollection with DI support (the other library uses reflection)?
One hopes in-time that (as other libraries) the DI integration is not tightly integrated such that one can use different services containers (albeit in the other library once would need to write a custom
Convention
vs the included DI one to achieve this) ?The text was updated successfully, but these errors were encountered: