From efae4899ce74101a09f5a0a90722bd7ce0dabca4 Mon Sep 17 00:00:00 2001 From: maliming Date: Sun, 26 Dec 2021 14:39:21 +0800 Subject: [PATCH] Add `WebApplicationBuilderExtensions`. --- .../AbpWebAssemblyHostBuilderExtensions.cs | 8 ++--- .../WebApplicationBuilderExtensions.cs | 35 +++++++++++++++++++ .../Volo/Abp/AbpApplicationFactory.cs | 8 ++--- .../Program.cs | 5 +-- .../Program.cs | 5 +-- .../DbMigratorHostedService.cs | 17 +++++---- .../Program.cs | 5 +-- .../Program.cs | 5 +-- .../Program.cs | 5 +-- .../Program.cs | 5 +-- .../Program.cs | 5 +-- .../ConsoleTestAppHostedService.cs | 15 ++++---- .../Program.cs | 5 +-- .../Program.cs | 5 +-- .../Program.cs | 5 +-- .../Program.cs | 5 +-- .../Program.cs | 5 +-- .../ConsoleTestAppHostedService.cs | 15 ++++---- 18 files changed, 83 insertions(+), 75 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/WebApplicationBuilderExtensions.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/AspNetCore/Components/WebAssembly/Hosting/AbpWebAssemblyHostBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/AspNetCore/Components/WebAssembly/Hosting/AbpWebAssemblyHostBuilderExtensions.cs index 7c4ff4a868..b92bb96f70 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/AspNetCore/Components/WebAssembly/Hosting/AbpWebAssemblyHostBuilderExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/AspNetCore/Components/WebAssembly/Hosting/AbpWebAssemblyHostBuilderExtensions.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting; public static class AbpWebAssemblyHostBuilderExtensions { - public static async Task AddApplicationAsync( + public async static Task AddApplicationAsync( [NotNull] this WebAssemblyHostBuilder builder, Action options) where TStartupModule : IAbpModule @@ -65,7 +65,7 @@ public static class AbpWebAssemblyHostBuilderExtensions return application; } - public static async Task InitializeAsync( + public async static Task InitializeAsync( [NotNull] this IAbpApplicationWithExternalServiceProvider application, [NotNull] IServiceProvider serviceProvider) { @@ -80,7 +80,7 @@ public static class AbpWebAssemblyHostBuilderExtensions await SetCurrentLanguageAsync(serviceProvider); } - private static async Task InitializeModulesAsync(IServiceProvider serviceProvider) + private async static Task InitializeModulesAsync(IServiceProvider serviceProvider) { foreach (var service in serviceProvider.GetServices()) { @@ -88,7 +88,7 @@ public static class AbpWebAssemblyHostBuilderExtensions } } - private static async Task SetCurrentLanguageAsync(IServiceProvider serviceProvider) + private async static Task SetCurrentLanguageAsync(IServiceProvider serviceProvider) { var configurationClient = serviceProvider.GetRequiredService(); var utilsService = serviceProvider.GetRequiredService(); diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/WebApplicationBuilderExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/WebApplicationBuilderExtensions.cs new file mode 100644 index 0000000000..692825520b --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/WebApplicationBuilderExtensions.cs @@ -0,0 +1,35 @@ +using System; +using System.Threading.Tasks; +using JetBrains.Annotations; +using Microsoft.AspNetCore.Builder; +using Volo.Abp; +using Volo.Abp.Modularity; + +namespace Microsoft.Extensions.DependencyInjection; + +public static class WebApplicationBuilderExtensions +{ + public async static Task AddApplicationAsync( + [NotNull] this WebApplicationBuilder builder, + [CanBeNull] Action optionsAction = null) + where TStartupModule : IAbpModule + { + return await builder.Services.AddApplicationAsync(options => + { + options.Services.ReplaceConfiguration(builder.Configuration); + optionsAction?.Invoke(options); + }); + } + + public async static Task AddApplicationAsync( + [NotNull] this WebApplicationBuilder builder, + [NotNull] Type startupModuleType, + [CanBeNull] Action optionsAction = null) + { + return await builder.Services.AddApplicationAsync(startupModuleType, options => + { + options.Services.ReplaceConfiguration(builder.Configuration); + optionsAction?.Invoke(options); + }); + } +} diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs index 50e5e862ca..3d64b39b7c 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationFactory.cs @@ -14,8 +14,8 @@ public static class AbpApplicationFactory { var app = Create(typeof(TStartupModule), options => { - optionsAction?.Invoke(options); options.SkipConfigureServices = true; + optionsAction?.Invoke(options); }); await app.ConfigureServicesAsync(); return app; @@ -27,8 +27,8 @@ public static class AbpApplicationFactory { var app = new AbpApplicationWithInternalServiceProvider(startupModuleType, options => { - optionsAction?.Invoke(options); options.SkipConfigureServices = true; + optionsAction?.Invoke(options); }); await app.ConfigureServicesAsync(); return app; @@ -41,8 +41,8 @@ public static class AbpApplicationFactory { var app = Create(typeof(TStartupModule), services, options => { - optionsAction?.Invoke(options); options.SkipConfigureServices = true; + optionsAction?.Invoke(options); }); await app.ConfigureServicesAsync(); return app; @@ -55,8 +55,8 @@ public static class AbpApplicationFactory { var app = new AbpApplicationWithExternalServiceProvider(startupModuleType, services, options => { - optionsAction?.Invoke(options); options.SkipConfigureServices = true; + optionsAction?.Invoke(options); }); await app.ConfigureServicesAsync(); return app; diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.Tiered/Program.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.Tiered/Program.cs index 838c64e004..d028aeafad 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.Tiered/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.Tiered/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/Program.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/Program.cs index b9aad1a4cd..8cffe96c42 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/DbMigratorHostedService.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/DbMigratorHostedService.cs index ca084c1833..0b31a64ddd 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/DbMigratorHostedService.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.DbMigrator/DbMigratorHostedService.cs @@ -22,25 +22,28 @@ public class DbMigratorHostedService : IHostedService public async Task StartAsync(CancellationToken cancellationToken) { - using (var application = AbpApplicationFactory.Create(options => + using (var application = await AbpApplicationFactory.CreateAsync(options => { - options.Services.ReplaceConfiguration(_configuration); - options.UseAutofac(); - options.Services.AddLogging(c => c.AddSerilog()); + options.Services.ReplaceConfiguration(_configuration); + options.UseAutofac(); + options.Services.AddLogging(c => c.AddSerilog()); })) { - application.Initialize(); + await application.InitializeAsync(); await application .ServiceProvider .GetRequiredService() .MigrateAsync(); - application.Shutdown(); + await application.ShutdownAsync(); _hostApplicationLifetime.StopApplication(); } } - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } } diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/Program.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/Program.cs index 59b85f1a90..b3bb9299ba 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.Host/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.HostWithIds/Program.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.HostWithIds/Program.cs index 59b85f1a90..b3bb9299ba 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.HostWithIds/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.HttpApi.HostWithIds/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/Program.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/Program.cs index aca0769330..2699dc2993 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/Program.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/Program.cs index 3cd8f691dc..8c345c70a2 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web.Host/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web/Program.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web/Program.cs index 3cd8f691dc..8c345c70a2 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs b/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs index 53eab979d4..b756d400f8 100644 --- a/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs +++ b/templates/app/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs @@ -18,20 +18,23 @@ public class ConsoleTestAppHostedService : IHostedService public async Task StartAsync(CancellationToken cancellationToken) { - using (var application = AbpApplicationFactory.Create(options => + using (var application = await AbpApplicationFactory.CreateAsync(options => { - options.Services.ReplaceConfiguration(_configuration); - options.UseAutofac(); + options.Services.ReplaceConfiguration(_configuration); + options.UseAutofac(); })) { - application.Initialize(); + await application.InitializeAsync(); var demo = application.ServiceProvider.GetRequiredService(); await demo.RunAsync(); - application.Shutdown(); + await application.ShutdownAsync(); } } - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } } diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Program.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Program.cs index c1b5c178d5..cff77845f1 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Program.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Program.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Program.cs index 328016c3b0..a315163da3 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Program.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Program.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Program.cs index 5f5b29332b..540e2312e2 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Program.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Program.cs @@ -34,10 +34,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Program.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Program.cs index cdeeed1546..2d54ded331 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Program.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Program.cs @@ -37,10 +37,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Program.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Program.cs index a8443ddaca..f8d7f828c5 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Program.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Program.cs @@ -37,10 +37,7 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - await builder.Services.AddApplicationAsync(options => - { - options.Services.ReplaceConfiguration(builder.Configuration); - }); + await builder.AddApplicationAsync(); var app = builder.Build(); await app.InitializeApplicationAsync(); await app.RunAsync(); diff --git a/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs b/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs index 404248dda8..64b17070d6 100644 --- a/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs +++ b/templates/module/aspnet-core/test/MyCompanyName.MyProjectName.HttpApi.Client.ConsoleTestApp/ConsoleTestAppHostedService.cs @@ -18,20 +18,23 @@ public class ConsoleTestAppHostedService : IHostedService public async Task StartAsync(CancellationToken cancellationToken) { - using (var application = AbpApplicationFactory.Create(options => + using (var application = await AbpApplicationFactory.CreateAsync(options => { - options.Services.ReplaceConfiguration(_configuration); - options.UseAutofac(); + options.Services.ReplaceConfiguration(_configuration); + options.UseAutofac(); })) { - application.Initialize(); + await application.InitializeAsync(); var demo = application.ServiceProvider.GetRequiredService(); await demo.RunAsync(); - application.Shutdown(); + await application.ShutdownAsync(); } } - public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } }