From 01a8c1e9fbb51585822e94ea5c99c6b626722e6e Mon Sep 17 00:00:00 2001 From: NikiforovAll Date: Thu, 28 May 2020 22:02:15 +0300 Subject: [PATCH 1/3] Add GenericHost --- samples/HostingPlayground/Greeter.cs | 7 +++ samples/HostingPlayground/GreeterOptions.cs | 12 +++++ .../HostingPlayground.csproj | 23 ++++++++ .../HostingPlaygroundLogEvents.cs | 7 +++ samples/HostingPlayground/IGreeter.cs | 9 ++++ samples/HostingPlayground/Program.cs | 52 +++++++++++++++++++ samples/HostingPlayground/appsettings.json | 8 +++ 7 files changed, 118 insertions(+) create mode 100644 samples/HostingPlayground/Greeter.cs create mode 100644 samples/HostingPlayground/GreeterOptions.cs create mode 100644 samples/HostingPlayground/HostingPlayground.csproj create mode 100644 samples/HostingPlayground/HostingPlaygroundLogEvents.cs create mode 100644 samples/HostingPlayground/IGreeter.cs create mode 100644 samples/HostingPlayground/Program.cs create mode 100644 samples/HostingPlayground/appsettings.json diff --git a/samples/HostingPlayground/Greeter.cs b/samples/HostingPlayground/Greeter.cs new file mode 100644 index 0000000000..9dc6f335b9 --- /dev/null +++ b/samples/HostingPlayground/Greeter.cs @@ -0,0 +1,7 @@ +namespace HostingPlayground +{ + public class Greeter : IGreeter + { + + } +} diff --git a/samples/HostingPlayground/GreeterOptions.cs b/samples/HostingPlayground/GreeterOptions.cs new file mode 100644 index 0000000000..dc3088420d --- /dev/null +++ b/samples/HostingPlayground/GreeterOptions.cs @@ -0,0 +1,12 @@ +namespace HostingPlayground +{ + public class GreeterOptions + { + public string Name { get; set; } + + public GreeterOptions(string name) + { + Name = name; + } + } +} diff --git a/samples/HostingPlayground/HostingPlayground.csproj b/samples/HostingPlayground/HostingPlayground.csproj new file mode 100644 index 0000000000..91b6ffe19d --- /dev/null +++ b/samples/HostingPlayground/HostingPlayground.csproj @@ -0,0 +1,23 @@ + + + + Exe + netcoreapp3.1 + true + latest + + + + + + + + + + + + + + + + diff --git a/samples/HostingPlayground/HostingPlaygroundLogEvents.cs b/samples/HostingPlayground/HostingPlaygroundLogEvents.cs new file mode 100644 index 0000000000..de4a513d6b --- /dev/null +++ b/samples/HostingPlayground/HostingPlaygroundLogEvents.cs @@ -0,0 +1,7 @@ +namespace HostingPlayground +{ + public class HostingPlaygroundLogEvents + { + public const int GreetEvent = 1000; + } +} diff --git a/samples/HostingPlayground/IGreeter.cs b/samples/HostingPlayground/IGreeter.cs new file mode 100644 index 0000000000..b5d737ee6e --- /dev/null +++ b/samples/HostingPlayground/IGreeter.cs @@ -0,0 +1,9 @@ +using System; + +namespace HostingPlayground +{ + interface IGreeter + { + void Greet(string name) => Console.WriteLine($"Hello, {name ?? "anonymous"}"); + } +} diff --git a/samples/HostingPlayground/Program.cs b/samples/HostingPlayground/Program.cs new file mode 100644 index 0000000000..2d190d4790 --- /dev/null +++ b/samples/HostingPlayground/Program.cs @@ -0,0 +1,52 @@ +using System.CommandLine; +using System.CommandLine.Builder; +using System.CommandLine.Hosting; +using System.CommandLine.Invocation; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using System.CommandLine.Parsing; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using static HostingPlayground.HostingPlaygroundLogEvents; + +namespace HostingPlayground +{ + class Program + { + static async Task Main(string[] args) => await BuildCommandLine() + .UseHost(_ => Host.CreateDefaultBuilder(), + host => + { + host.ConfigureServices(services => + { + services.AddSingleton(); + }); + }) + .UseDefaults() + .Build() + .InvokeAsync(args); + + private static CommandLineBuilder BuildCommandLine() + { + var root = new RootCommand(@"$ dotnet run --name 'Joe'"){ + new Option("--name"){ + Required = true + } + }; + root.Handler = CommandHandler.Create(Run); + return new CommandLineBuilder(root); + } + + private static void Run(GreeterOptions options, IHost host) + { + var serviceProvider = host.Services; + var greeter = serviceProvider.GetRequiredService(); + var loggerFactory = serviceProvider.GetRequiredService(); + var logger = loggerFactory.CreateLogger(typeof(Program)); + + var name = options.Name; + logger.LogInformation(GreetEvent, "Greeting was requested for: {name}", name); + greeter.Greet(name); + } + } +} diff --git a/samples/HostingPlayground/appsettings.json b/samples/HostingPlayground/appsettings.json new file mode 100644 index 0000000000..7cb2ee756f --- /dev/null +++ b/samples/HostingPlayground/appsettings.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning" + } + } +} From 6d6e776acd9fa964835468008bcfaea09591cf7b Mon Sep 17 00:00:00 2001 From: NikiforovAll Date: Thu, 28 May 2020 22:11:05 +0300 Subject: [PATCH 2/3] Add HostingPlayground to solution file --- System.CommandLine.sln | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/System.CommandLine.sln b/System.CommandLine.sln index ec970108de..bb83d4f820 100644 --- a/System.CommandLine.sln +++ b/System.CommandLine.sln @@ -56,6 +56,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.CommandLine.Hosting" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.CommandLine.Hosting.Tests", "src\System.CommandLine.Hosting.Tests\System.CommandLine.Hosting.Tests.csproj", "{39483140-BC26-4CAD-BBAE-3DC76C2F16CF}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HostingPlayground", "samples\HostingPlayground\HostingPlayground.csproj", "{0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -222,6 +224,18 @@ Global {39483140-BC26-4CAD-BBAE-3DC76C2F16CF}.Release|x64.Build.0 = Release|Any CPU {39483140-BC26-4CAD-BBAE-3DC76C2F16CF}.Release|x86.ActiveCfg = Release|Any CPU {39483140-BC26-4CAD-BBAE-3DC76C2F16CF}.Release|x86.Build.0 = Release|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x64.ActiveCfg = Debug|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x64.Build.0 = Debug|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x86.ActiveCfg = Debug|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Debug|x86.Build.0 = Debug|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|Any CPU.Build.0 = Release|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x64.ActiveCfg = Release|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x64.Build.0 = Release|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x86.ActiveCfg = Release|Any CPU + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -240,6 +254,7 @@ Global {C39B0705-993E-43DB-B66A-A37A587F0BF7} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45} {644C4B4A-4A32-4307-9F71-C3BF901FFB66} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45} {39483140-BC26-4CAD-BBAE-3DC76C2F16CF} = {E5B1EC71-0FC4-4FAA-9C65-32D5016FBC45} + {0BF6958D-9EE3-4623-B3D6-4DA77EAC1906} = {6749FB3E-39DE-4321-A39E-525278E9408D} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {5C159F93-800B-49E7-9905-EE09F8B8434A} From 2ed08afc9d510123e00f340224b182826526a3e5 Mon Sep 17 00:00:00 2001 From: NikiforovAll Date: Sat, 30 May 2020 12:38:47 +0300 Subject: [PATCH 3/3] adjust langversion; readonly greeter options --- samples/HostingPlayground/GreeterOptions.cs | 2 +- samples/HostingPlayground/HostingPlayground.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/HostingPlayground/GreeterOptions.cs b/samples/HostingPlayground/GreeterOptions.cs index dc3088420d..ba56d27dfc 100644 --- a/samples/HostingPlayground/GreeterOptions.cs +++ b/samples/HostingPlayground/GreeterOptions.cs @@ -2,7 +2,7 @@ namespace HostingPlayground { public class GreeterOptions { - public string Name { get; set; } + public string Name { get; } public GreeterOptions(string name) { diff --git a/samples/HostingPlayground/HostingPlayground.csproj b/samples/HostingPlayground/HostingPlayground.csproj index 91b6ffe19d..a708c390e4 100644 --- a/samples/HostingPlayground/HostingPlayground.csproj +++ b/samples/HostingPlayground/HostingPlayground.csproj @@ -4,7 +4,7 @@ Exe netcoreapp3.1 true - latest + 8