@@ -199,6 +199,12 @@ public static Task RunAsync(string[] args)
199199
200200 public static ConsoleAppBuilder Create() => new ConsoleAppBuilder();
201201
202+ public static ConsoleAppBuilder Create(IServiceProvider serviceProvider)
203+ {
204+ ConsoleApp.ServiceProvider = serviceProvider;
205+ return ConsoleApp.Create();
206+ }
207+
202208 static void ThrowArgumentParseFailed(string argumentName, string value)
203209 {
204210 throw new ArgumentParseFailedException($"Argument '{argumentName}' failed to parse, provided value: {value}");
@@ -667,6 +673,26 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
667673 emitter . EmitHelp ( help , commandIds ! ) ;
668674 }
669675 sourceProductionContext . AddSource ( "ConsoleApp.Builder.Help.g.cs" , help . ToString ( ) ) ;
676+
677+ // emit Create(IServiceCollection) if exists Microsoft.Extensions.DependencyInjection.ServiceCollection
678+ if ( collectBuilderContext . HasDependencyInjectionReference )
679+ {
680+ var runBuilder = new SourceBuilder ( 0 ) ;
681+ runBuilder . AppendLine ( GeneratedCodeHeader ) ;
682+ runBuilder . AppendLine ( "using Microsoft.Extensions.DependencyInjection" ) ;
683+ runBuilder . AppendLine ( ) ;
684+ using ( runBuilder . BeginBlock ( "internal static partial class ConsoleApp" ) )
685+ {
686+ using ( runBuilder . BeginBlock ( "public static ConsoleAppBuilder Create(Action<IServiceCollection> configure)" ) )
687+ {
688+ runBuilder . AppendLine ( "var services = new ServiceCollection();" ) ;
689+ runBuilder . AppendLine ( "configure(services);" ) ;
690+ runBuilder . AppendLine ( "ConsoleApp.ServiceProvider = services.BuildServiceProvider();" ) ;
691+ runBuilder . AppendLine ( "return ConsoleApp.Create();" ) ;
692+ }
693+ }
694+ sourceProductionContext . AddSource ( "ConsoleApp.Builder.Run.g.cs" , runBuilder . ToString ( ) ) ;
695+ }
670696 }
671697
672698 class CommanContext ( Command ? command , bool isAsync , DiagnosticReporter diagnosticReporter , InvocationExpressionSyntax node ) : IEquatable < CommanContext >
@@ -694,6 +720,7 @@ class CollectBuilderContext : IEquatable<CollectBuilderContext>
694720 public CancellationToken CancellationToken { get ; }
695721 public bool HasRun { get ; }
696722 public bool HasRunAsync { get ; }
723+ public bool HasDependencyInjectionReference { get ; }
697724
698725 public CollectBuilderContext ( ImmutableArray < BuilderContext > contexts , CancellationToken cancellationToken )
699726 {
@@ -805,13 +832,18 @@ public CollectBuilderContext(ImmutableArray<BuilderContext> contexts, Cancellati
805832 this . Commands = commands1 . Concat ( commands2 ! ) . ToArray ( ) ! ; // not null if no diagnostics
806833 this . HasRun = methodGroup [ "Run" ] . Any ( ) ;
807834 this . HasRunAsync = methodGroup [ "RunAsync" ] . Any ( ) ;
835+
836+ var model = contexts [ 0 ] . Model ;
837+ var serviceCollectionSymbol = model . Compilation . GetTypeByMetadataName ( "Microsoft.Extensions.DependencyInjection.ServiceCollection" ) ;
838+ this . HasDependencyInjectionReference = serviceCollectionSymbol != null ;
808839 }
809840
810841 public bool Equals ( CollectBuilderContext other )
811842 {
812843 if ( DiagnosticReporter . HasDiagnostics || other . DiagnosticReporter . HasDiagnostics ) return false ;
813844 if ( HasRun != other . HasRun ) return false ;
814845 if ( HasRunAsync != other . HasRunAsync ) return false ;
846+ if ( HasDependencyInjectionReference != other . HasDependencyInjectionReference ) return false ;
815847
816848 return Commands . AsSpan ( ) . SequenceEqual ( other . Commands ) ;
817849 }
0 commit comments