-
tye
Discontinued Tye is a tool that makes developing, testing, and deploying microservices and distributed applications easier. Project Tye includes a local orchestrator to make developing microservices easier and the ability to deploy microservices to Kubernetes with minimal configuration.
-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
System.CommandLine is the official .NET library that provides common functionality for command-line applications. This includes features like argument parsing, automatic help text generation, tab autocomplete, suggestions, corrections, sub-commands, user cancellation, and much more. Many official .NET tools are built on top of System.CommandLine, including the .NET CLI, Kiota, Tye, numerous Azure tools, and other .NET additional tools.
using System.CommandLine.Invocation; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace System.CommandLine.Builder; internal static class DependencyInjectionMiddleware { public static CommandLineBuilder UseDependencyInjection(this CommandLineBuilder builder, Action configureServices) { return UseDependencyInjection(builder, (_, services) => configureServices(services)); } // This overload allows you to conditionally register services based on the command line invocation context // in order to improve startup time when you have a lot of services to register. public static CommandLineBuilder UseDependencyInjection(this CommandLineBuilder builder, Action configureServices) { return builder.AddMiddleware(async (context, next) => { // Register our services in the modern Microsoft dependency injection container var services = new ServiceCollection(); configureServices(context, services); var uniqueServiceTypes = new HashSet(services.Select(x => x.ServiceType)); services.TryAddSingleton(context.Console); await using var serviceProvider = services.BuildServiceProvider(); // System.CommandLine's service provider is a "fake" implementation that relies on a dictionary of factories, // but we can still make sure here that "true" dependency-injected services are available from "context.BindingContext". // https://github.com/dotnet/command-line-api/blob/2.0.0-beta4.22272.1/src/System.CommandLine/Invocation/ServiceProvider.cs context.BindingContext.AddService(_ => serviceProvider); foreach (var serviceType in uniqueServiceTypes) { context.BindingContext.AddService(serviceType, _ => serviceProvider.GetRequiredService(serviceType)); // Enable support for "context.BindingContext.GetServices<>()" as in the modern dependency injection var enumerableServiceType = typeof(IEnumerable<>).MakeGenericType(serviceType); context.BindingContext.AddService(enumerableServiceType, _ => serviceProvider.GetServices(serviceType)); } await next(context); }); } }
Fortunately, System.CommandLine (in its current version, 2.0.0-beta4.22272.1) is highly extensible. In this blog post, I'll show you how to integrate true dependency injection using the official Microsoft.Extensions.DependencyInjection NuGet package.
Related posts
-
C# .NET Tools with System.CommandLine
-
Pro .NET Console Apps
-
Show HN: Replbuilder, quickly build a Python REPL CLI prompt
-
Fellow C# coders, I was randomly making a RANDOM script just for fun, when all of a sudden, an error popped up! For some reason, my program just can't detect a method called "Main" when it's right there! (Program does not contain a static "Main" method suitable for an entry point) Thank you!
-
Why would you write extension methods for the type you own?