From e2d9d7f668549b19cc2b085f80204e3209027cca Mon Sep 17 00:00:00 2001 From: StardustDL Date: Mon, 31 May 2021 11:10:13 +0800 Subject: [PATCH] add hosting package --- Modulight.sln | 24 ++++++++- README.md | 12 ++--- build/DeployPackageTask.cs | 1 + .../CommandLineModuleBuilderOptions.cs | 16 ++++++ .../CommandLineModuleExtensions.cs | 10 +++- .../CommandLineModuleHostingExtensions.cs | 53 ------------------- .../CommandLineModulePlugin.cs | 29 +++++++++- .../Modulight.Modules.CommandLine.csproj | 5 ++ src/Modulight.Modules.Core/README.md | 8 +-- .../ModuleHostingExtensions.cs} | 6 +-- .../Modulight.Modules.Hosting.csproj | 35 ++++++++++++ src/Modulight.Modules.Hosting/README.md | 9 ++++ .../Modulight.Modules.Server.AspNet.csproj | 2 +- src/Modulight.Modules.Server.AspNet/README.md | 2 +- .../Modulight.Modules.Test.csproj | 2 + src/Modulight.UI.Blazor.Hosting/README.md | 2 + test/Test.CommandLine/Program.cs | 2 +- 17 files changed, 145 insertions(+), 73 deletions(-) create mode 100644 src/Modulight.Modules.CommandLine/CommandLineModuleBuilderOptions.cs delete mode 100644 src/Modulight.Modules.CommandLine/CommandLineModuleHostingExtensions.cs rename src/{Modulight.Modules.Server.AspNet/AspNetServerHostingExtensions.cs => Modulight.Modules.Hosting/ModuleHostingExtensions.cs} (77%) create mode 100644 src/Modulight.Modules.Hosting/Modulight.Modules.Hosting.csproj create mode 100644 src/Modulight.Modules.Hosting/README.md diff --git a/Modulight.sln b/Modulight.sln index 8430d21..5501802 100644 --- a/Modulight.sln +++ b/Modulight.sln @@ -45,7 +45,17 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Delights.Modules.Hello", "s EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Modulight.Modules.CommandLine", "src\Modulight.Modules.CommandLine\Modulight.Modules.CommandLine.csproj", "{08D91884-F32A-4838-99C7-9E4F8FB605E9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test.CommandLine", "test\Test.CommandLine\Test.CommandLine.csproj", "{AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Test.CommandLine", "test\Test.CommandLine\Test.CommandLine.csproj", "{AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modulight.Modules.Hosting", "src\Modulight.Modules.Hosting\Modulight.Modules.Hosting.csproj", "{60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A4DA65C6-7D97-4C3D-85F0-BB5152691461}" + ProjectSection(SolutionItems) = preProject + .dockerignore = .dockerignore + .gitignore = .gitignore + LICENSE = LICENSE + README.md = README.md + EndProjectSection EndProject Global GlobalSection(SharedMSBuildProjectFiles) = preSolution @@ -266,6 +276,18 @@ Global {AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}.Release|x64.Build.0 = Release|Any CPU {AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}.Release|x86.ActiveCfg = Release|Any CPU {AA8B4F34-F756-4ADC-B869-9F8A5695E2C0}.Release|x86.Build.0 = Release|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|x64.ActiveCfg = Debug|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|x64.Build.0 = Debug|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|x86.ActiveCfg = Debug|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Debug|x86.Build.0 = Debug|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|Any CPU.Build.0 = Release|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|x64.ActiveCfg = Release|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|x64.Build.0 = Release|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|x86.ActiveCfg = Release|Any CPU + {60A21C6F-1B3E-4157-AB7C-6DB9FB27BCF6}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/README.md b/README.md index 3519af1..ccb7fe0 100644 --- a/README.md +++ b/README.md @@ -28,29 +28,29 @@ ### Use modules -See [README](./src/Modulight.Modules.Core/README.md) for details. +See [here](./src/Modulight.Modules.Core/README.md) for details. ### Addition steps #### Use Command line modules -See [README](./src/Modulight.Modules.CommandLine/README.md) for details. +See [here](./src/Modulight.Modules.CommandLine/README.md) for details. #### Use Razor component modules -See [README](./src/Modulight.Modules.Client.RazorComponents/README.md) for details. +See [here](./src/Modulight.Modules.Client.RazorComponents/README.md) for details. #### Use Blazor UI hosting template -See [README](./src/Modulight.UI.Blazor/README.md) for details. +See [here](./src/Modulight.UI.Blazor/README.md) for details. #### Use ASP.NET modules -See [README](./src/Modulight.Modules.Server.AspNet/README.md) for details. +See [here](./src/Modulight.Modules.Server.AspNet/README.md) for details. #### Use GraphQL modules -See [README](./src/Modulight.Modules.Server.GraphQL/README.md) for details. +See [here](./src/Modulight.Modules.Server.GraphQL/README.md) for details. ## Example codes diff --git a/build/DeployPackageTask.cs b/build/DeployPackageTask.cs index f60a364..7b46cc9 100644 --- a/build/DeployPackageTask.cs +++ b/build/DeployPackageTask.cs @@ -84,6 +84,7 @@ public override void Finally(BuildContext context) readonly List ModulightPackages = new List { "Modulight.Modules.Core", + "Modulight.Modules.Hosting", "Modulight.Modules.CommandLine", "Modulight.Modules.Client.RazorComponents", "Modulight.Modules.Server.AspNet", diff --git a/src/Modulight.Modules.CommandLine/CommandLineModuleBuilderOptions.cs b/src/Modulight.Modules.CommandLine/CommandLineModuleBuilderOptions.cs new file mode 100644 index 0000000..46ef430 --- /dev/null +++ b/src/Modulight.Modules.CommandLine/CommandLineModuleBuilderOptions.cs @@ -0,0 +1,16 @@ +using Microsoft.Extensions.Hosting; +using System; + +namespace Modulight.Modules.CommandLine +{ + /// + /// + /// + public class CommandLineModuleBuilderOptions + { + /// + /// Default to true. + /// + public bool SuppressStatusMessages { get; set; } = true; + } +} \ No newline at end of file diff --git a/src/Modulight.Modules.CommandLine/CommandLineModuleExtensions.cs b/src/Modulight.Modules.CommandLine/CommandLineModuleExtensions.cs index 0b9afed..730eb07 100644 --- a/src/Modulight.Modules.CommandLine/CommandLineModuleExtensions.cs +++ b/src/Modulight.Modules.CommandLine/CommandLineModuleExtensions.cs @@ -18,12 +18,18 @@ public static class CommandLineModuleExtensions /// Use building plugin for commandline modules. /// /// + /// /// - public static IModuleHostBuilder UseCommandLineModules(this IModuleHostBuilder modules) + public static IModuleHostBuilder UseCommandLineModules(this IModuleHostBuilder modules, Action? configureOptions = null) { return modules.ConfigureBuilderServices(services => { services.TryAddTransient(); + var optionsBuilder = services.AddOptions(); + if (configureOptions is not null) + { + optionsBuilder.Configure(configureOptions); + } }).UsePlugin().AddModule(); } @@ -98,7 +104,7 @@ public static ICommandLineModuleManifestBuilder WithDefaultsFromModuleType(this var cmdAttrs = type.GetCustomAttributes(true); - foreach(var attr in cmdAttrs) + foreach (var attr in cmdAttrs) { builder.AddCommand(attr.Type); } diff --git a/src/Modulight.Modules.CommandLine/CommandLineModuleHostingExtensions.cs b/src/Modulight.Modules.CommandLine/CommandLineModuleHostingExtensions.cs deleted file mode 100644 index 3c1db29..0000000 --- a/src/Modulight.Modules.CommandLine/CommandLineModuleHostingExtensions.cs +++ /dev/null @@ -1,53 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Modulight.Modules.CommandLine; -using System; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; - -namespace Microsoft.Extensions.Hosting -{ - /// - /// Extension methods for commandline hosting. - /// - public static class CommandLineModuleHostingExtensions - { - /// - /// - /// - /// - /// - public static IHostBuilder AddCommandLineWorker(this IHostBuilder hostBuilder) => hostBuilder.ConfigureServices( - (hostContext, services) => - { - services.AddHostedService(); - }); - - /// - /// Run with modules - /// - /// - /// - /// - public static async Task RunConsoleAsyncWithModules(this IHostBuilder hostBuilder, bool? suppressStatusMessages = null) - { - hostBuilder = hostBuilder.AddCommandLineWorker(); - - if (suppressStatusMessages is true) - { - hostBuilder.ConfigureLogging(builder => - { - builder.AddFilter("Modulight.Modules.Hosting", LogLevel.Warning); - }); - } - - hostBuilder = suppressStatusMessages is null ? hostBuilder.UseConsoleLifetime() : - hostBuilder.UseConsoleLifetime(o => o.SuppressStatusMessages = suppressStatusMessages.Value); - - var host = hostBuilder.Build(); - - await using var _ = await host.Services.UseModules(); - - await host.RunAsync(); - } - } -} \ No newline at end of file diff --git a/src/Modulight.Modules.CommandLine/CommandLineModulePlugin.cs b/src/Modulight.Modules.CommandLine/CommandLineModulePlugin.cs index 5ebf836..fb2eb7b 100644 --- a/src/Modulight.Modules.CommandLine/CommandLineModulePlugin.cs +++ b/src/Modulight.Modules.CommandLine/CommandLineModulePlugin.cs @@ -2,6 +2,8 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting.Internal; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Modulight.Modules.CommandLine; using Modulight.Modules.Hosting; using System; @@ -11,10 +13,35 @@ namespace Modulight.Modules.CommandLine { internal sealed class CommandLineModulePlugin : ModuleHostBuilderPlugin { - public CommandLineModulePlugin(IServiceProvider builderServices) => BuilderServices = builderServices; + public CommandLineModulePlugin(IServiceProvider builderServices, IOptions options) + { + BuilderServices = builderServices; + Options = options.Value; + } IServiceProvider BuilderServices { get; } + public CommandLineModuleBuilderOptions Options { get; } + + public override void AfterBuild(ModuleDefinition[] modules, IServiceCollection services) + { + services.AddHostedService(); + + if (Options.SuppressStatusMessages is true) + { + services.AddLogging(builder => + { + builder.AddFilter("Modulight.Modules.Hosting", LogLevel.Warning); + }); + } + + // From HostBuilder.UseConsoleLifetime + services.AddSingleton(); + services.Configure(o => o.SuppressStatusMessages = Options.SuppressStatusMessages); + + base.AfterBuild(modules, services); + } + public override void AfterModule(ModuleDefinition module, IServiceCollection services) { if (module.Type.IsModule()) diff --git a/src/Modulight.Modules.CommandLine/Modulight.Modules.CommandLine.csproj b/src/Modulight.Modules.CommandLine/Modulight.Modules.CommandLine.csproj index f1effd3..d659cc3 100644 --- a/src/Modulight.Modules.CommandLine/Modulight.Modules.CommandLine.csproj +++ b/src/Modulight.Modules.CommandLine/Modulight.Modules.CommandLine.csproj @@ -27,6 +27,11 @@ + + + + + diff --git a/src/Modulight.Modules.Core/README.md b/src/Modulight.Modules.Core/README.md index af010fc..50ef2d5 100644 --- a/src/Modulight.Modules.Core/README.md +++ b/src/Modulight.Modules.Core/README.md @@ -10,7 +10,7 @@ For general modules: -```cs +```csharp services.AddModules(builder => { builder.AddModule(); }); @@ -18,7 +18,7 @@ services.AddModules(builder => { 2. Configure the module initilizing & shutdown. -```cs +```csharp var host = services.GetModuleHost(); await host.Initialize(); @@ -36,8 +36,8 @@ await using var _ = await services.UseModuleHost(); Or use extension methods for hosting: -```cs -// ASP.NET hosting. (provided by package Modulight.Modules.Server.AspNet) +```csharp +// Generic hosting. (provided by package Modulight.Modules.Hosting) // in Program: Task Main(string[] args) await CreateHostBuilder(args).Build().RunAsyncWithModules(); diff --git a/src/Modulight.Modules.Server.AspNet/AspNetServerHostingExtensions.cs b/src/Modulight.Modules.Hosting/ModuleHostingExtensions.cs similarity index 77% rename from src/Modulight.Modules.Server.AspNet/AspNetServerHostingExtensions.cs rename to src/Modulight.Modules.Hosting/ModuleHostingExtensions.cs index 59ea4fa..11c9e71 100644 --- a/src/Modulight.Modules.Server.AspNet/AspNetServerHostingExtensions.cs +++ b/src/Modulight.Modules.Hosting/ModuleHostingExtensions.cs @@ -1,13 +1,13 @@ using Microsoft.Extensions.DependencyInjection; -using Modulight.Modules.Server.AspNet; +using System; using System.Threading.Tasks; namespace Microsoft.Extensions.Hosting { /// - /// Extension methods for Asp.NET hosting. + /// Extension methods for module hosting. /// - public static class AspNetServerModuleHostingExtensions + public static class ModuleHostingExtensions { /// /// Run with modules diff --git a/src/Modulight.Modules.Hosting/Modulight.Modules.Hosting.csproj b/src/Modulight.Modules.Hosting/Modulight.Modules.Hosting.csproj new file mode 100644 index 0000000..da1f4dc --- /dev/null +++ b/src/Modulight.Modules.Hosting/Modulight.Modules.Hosting.csproj @@ -0,0 +1,35 @@ + + + + net5.0 + true + enable + true + MPL-2.0 + StardustDL + true + Hosting for Modulight modular framework. + https://github.com/StardustDL/modulight + https://github.com/StardustDL/modulight + Git + README.md + modular;hosting + + + + + + + + + + + + + + + + + + + diff --git a/src/Modulight.Modules.Hosting/README.md b/src/Modulight.Modules.Hosting/README.md new file mode 100644 index 0000000..05b2671 --- /dev/null +++ b/src/Modulight.Modules.Hosting/README.md @@ -0,0 +1,9 @@ +# Modulight.Modules.Hosting + +[![](https://buildstats.info/nuget/Modulight.Modules.Hosting)](https://www.nuget.org/packages/Modulight.Modules.Hosting/) + +```csharp +// Generic hosting. (provided by package Modulight.Modules.Hosting) +// in Program: Task Main(string[] args) +await CreateHostBuilder(args).Build().RunAsyncWithModules(); +``` \ No newline at end of file diff --git a/src/Modulight.Modules.Server.AspNet/Modulight.Modules.Server.AspNet.csproj b/src/Modulight.Modules.Server.AspNet/Modulight.Modules.Server.AspNet.csproj index 58c1426..68003ec 100644 --- a/src/Modulight.Modules.Server.AspNet/Modulight.Modules.Server.AspNet.csproj +++ b/src/Modulight.Modules.Server.AspNet/Modulight.Modules.Server.AspNet.csproj @@ -25,7 +25,7 @@ - + diff --git a/src/Modulight.Modules.Server.AspNet/README.md b/src/Modulight.Modules.Server.AspNet/README.md index 51f3634..5d08345 100644 --- a/src/Modulight.Modules.Server.AspNet/README.md +++ b/src/Modulight.Modules.Server.AspNet/README.md @@ -2,7 +2,7 @@ [![](https://buildstats.info/nuget/Modulight.Modules.Server.AspNet)](https://www.nuget.org/packages/Modulight.Modules.Server.AspNet/) -```cs +```csharp // in Startup: void ConfigureServices(ISeviceCollection services) services.AddModules(builder => { diff --git a/src/Modulight.Modules.Test/Modulight.Modules.Test.csproj b/src/Modulight.Modules.Test/Modulight.Modules.Test.csproj index c0df299..fa8afcf 100644 --- a/src/Modulight.Modules.Test/Modulight.Modules.Test.csproj +++ b/src/Modulight.Modules.Test/Modulight.Modules.Test.csproj @@ -12,7 +12,9 @@ + + diff --git a/src/Modulight.UI.Blazor.Hosting/README.md b/src/Modulight.UI.Blazor.Hosting/README.md index eb2776f..380d0be 100644 --- a/src/Modulight.UI.Blazor.Hosting/README.md +++ b/src/Modulight.UI.Blazor.Hosting/README.md @@ -1,5 +1,7 @@ # Modulight.UI.Blazor.Hosting +[Modulight.UI.Blazor.Hosting ![](https://buildstats.info/nuget/Modulight.UI.Blazor.Hosting?includePreReleases=true)](https://www.nuget.org/packages/Modulight.UI.Blazor.Hosting/) + ```cs // void ConfigureServices(IServiceCollection services) diff --git a/test/Test.CommandLine/Program.cs b/test/Test.CommandLine/Program.cs index 118025e..38f6a47 100644 --- a/test/Test.CommandLine/Program.cs +++ b/test/Test.CommandLine/Program.cs @@ -14,7 +14,7 @@ namespace Test.CommandLine { class Program { - public static async Task Main(string[] args) => await CreateHostBuilder(args).RunConsoleAsyncWithModules(true); + public static async Task Main(string[] args) => await CreateHostBuilder(args).Build().RunAsyncWithModules(); public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)