From 2212157f03c0fc1bbc99882052d1c732df277e5f Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 20 Dec 2021 17:59:53 +0800 Subject: [PATCH] Refactor. --- .../AbpAspNetCoreAsyncIntegratedTestBase.cs | 118 ++++++++++++++++++ .../TestBase/TestNoopHostLifetime.cs | 18 +++ .../AbpBackgroundJobsRabbitMqModule.cs | 37 +++--- .../BackgroundJobs/AbpBackgroundJobsModule.cs | 15 ++- .../HangfireBackgroundWorkerManager.cs | 2 +- .../AbpBackgroundWorkersQuartzModule.cs | 18 ++- .../Quartz/QuartzBackgroundWorkerManager.cs | 4 +- .../AbpBackgroundWorkersModule.cs | 25 +++- .../BackgroundWorkerManager.cs | 6 +- ...licationInitializationContextExtensions.cs | 11 +- .../IBackgroundWorkerManager.cs | 5 +- .../Volo/Abp/EventBus/AbpEventBusModule.cs | 12 +- .../Volo/Abp/Quartz/AbpQuartzModule.cs | 25 +++- .../Mvc/AbpAspNetCoreAsyncTestBase_Tests.cs | 62 +++++++++ .../AspNetCore/AbpAspNetCoreAsyncTestBase.cs | 45 +++++++ .../Volo/Docs/DocsDomainModule.cs | 15 ++- .../AbpIdentityServerDomainModule.cs | 23 +++- .../Program.cs | 9 +- .../Program.cs | 9 +- .../Program.cs | 2 +- .../Program.cs | 9 +- .../Program.cs | 9 +- .../Program.cs | 9 +- .../Program.cs | 9 +- .../Program.cs | 9 +- .../Program.cs | 2 +- .../MyProjectNameBlazorHostModule.cs | 12 +- .../Program.cs | 40 +++--- .../Startup.cs | 18 --- .../Program.cs | 41 +++--- .../Startup.cs | 20 --- .../MyProjectNameIdentityServerModule.cs | 20 ++- .../Program.cs | 41 +++--- .../Startup.cs | 20 --- .../Program.cs | 38 +++--- .../Startup.cs | 18 --- .../MyProjectNameWebUnifiedModule.cs | 12 +- .../Program.cs | 36 +++--- .../Startup.cs | 19 --- 39 files changed, 558 insertions(+), 285 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreAsyncIntegratedTestBase.cs create mode 100644 framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/TestNoopHostLifetime.cs create mode 100644 framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreAsyncTestBase_Tests.cs create mode 100644 framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreAsyncTestBase.cs delete mode 100644 templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Startup.cs delete mode 100644 templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Startup.cs delete mode 100644 templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Startup.cs delete mode 100644 templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Startup.cs delete mode 100644 templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Startup.cs diff --git a/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreAsyncIntegratedTestBase.cs b/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreAsyncIntegratedTestBase.cs new file mode 100644 index 0000000000..06a46897fa --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/AbpAspNetCoreAsyncIntegratedTestBase.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting.Server; +using Microsoft.AspNetCore.Routing; +using Microsoft.AspNetCore.TestHost; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Volo.Abp.Modularity; + +namespace Volo.Abp.AspNetCore.TestBase; + +public class AbpAspNetCoreAsyncIntegratedTestBase + where TModule : AbpModule +{ + protected IHost Host { get; set; } + + protected TestServer Server { get; set; } + + protected HttpClient Client { get; set; } + + protected IServiceProvider ServiceProvider { get; set; } + + protected virtual T GetService() + { + return ServiceProvider.GetService(); + } + + protected virtual T GetRequiredService() + { + return ServiceProvider.GetRequiredService(); + } + + public virtual async Task InitializeAsync() + { + var builder = WebApplication.CreateBuilder(); + builder.Host.ConfigureServices(services => + { + services.AddSingleton(); + services.AddSingleton(); + }) + .UseAutofac(); + + await builder.Services.AddApplicationAsync(options => + { + options.Services.ReplaceConfiguration(builder.Configuration); + }); + + await ConfigureServicesAsync(builder.Services); + + var app = builder.Build(); + + await app.InitializeApplicationAsync(); + + await app.StartAsync(); + + Host = app.Services.GetRequiredService(); + + Server = Host.GetTestServer(); + Client = Host.GetTestClient(); + + ServiceProvider = Server.Services; + + ServiceProvider.GetRequiredService().Server = Server; + } + + public virtual Task DisposeAsync() + { + return Task.CompletedTask; + } + + protected virtual Task ConfigureServicesAsync(IServiceCollection services) + { + return Task.CompletedTask; + } + + #region GetUrl + + /// + /// Gets default URL for given controller type. + /// + /// The type of the controller. + protected virtual string GetUrl() + { + return "/" + typeof(TController).Name.RemovePostFix("Controller", "AppService", "ApplicationService", "Service"); + } + + /// + /// Gets default URL for given controller type's given action. + /// + /// The type of the controller. + protected virtual string GetUrl(string actionName) + { + return GetUrl() + "/" + actionName; + } + + /// + /// Gets default URL for given controller type's given action with query string parameters (as anonymous object). + /// + /// The type of the controller. + protected virtual string GetUrl(string actionName, object queryStringParamsAsAnonymousObject) + { + var url = GetUrl(actionName); + + var dictionary = new RouteValueDictionary(queryStringParamsAsAnonymousObject); + if (dictionary.Any()) + { + url += "?" + dictionary.Select(d => $"{d.Key}={d.Value}").JoinAsString("&"); + } + + return url; + } + + #endregion +} diff --git a/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/TestNoopHostLifetime.cs b/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/TestNoopHostLifetime.cs new file mode 100644 index 0000000000..a0fe2d46c1 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.TestBase/Volo/Abp/AspNetCore/TestBase/TestNoopHostLifetime.cs @@ -0,0 +1,18 @@ +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Hosting; + +namespace Volo.Abp.AspNetCore.TestBase; + +public class TestNoopHostLifetime : IHostLifetime +{ + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + + public Task WaitForStartAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } +} diff --git a/framework/src/Volo.Abp.BackgroundJobs.RabbitMQ/Volo/Abp/BackgroundJobs/RabbitMQ/AbpBackgroundJobsRabbitMqModule.cs b/framework/src/Volo.Abp.BackgroundJobs.RabbitMQ/Volo/Abp/BackgroundJobs/RabbitMQ/AbpBackgroundJobsRabbitMqModule.cs index 33cbc740e4..c601ca5549 100644 --- a/framework/src/Volo.Abp.BackgroundJobs.RabbitMQ/Volo/Abp/BackgroundJobs/RabbitMQ/AbpBackgroundJobsRabbitMqModule.cs +++ b/framework/src/Volo.Abp.BackgroundJobs.RabbitMQ/Volo/Abp/BackgroundJobs/RabbitMQ/AbpBackgroundJobsRabbitMqModule.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Modularity; using Volo.Abp.RabbitMQ; using Volo.Abp.Threading; @@ -17,31 +18,37 @@ public class AbpBackgroundJobsRabbitMqModule : AbpModule context.Services.AddSingleton(typeof(IJobQueue<>), typeof(JobQueue<>)); } + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + await StartJobQueueManagerAsync(context); + } + + public async override Task OnApplicationShutdownAsync(ApplicationShutdownContext context) + { + await StopJobQueueManagerAsync(context); + } + public override void OnApplicationInitialization(ApplicationInitializationContext context) { - StartJobQueueManager(context); + AsyncHelper.RunSync(() => StartJobQueueManagerAsync(context)); } public override void OnApplicationShutdown(ApplicationShutdownContext context) { - StopJobQueueManager(context); + AsyncHelper.RunSync(() => StopJobQueueManagerAsync(context)); } - private static void StartJobQueueManager(ApplicationInitializationContext context) + private async static Task StartJobQueueManagerAsync(ApplicationInitializationContext context) { - AsyncHelper.RunSync( - () => context.ServiceProvider - .GetRequiredService() - .StartAsync() - ); + await context.ServiceProvider + .GetRequiredService() + .StartAsync(); } - private static void StopJobQueueManager(ApplicationShutdownContext context) + private async static Task StopJobQueueManagerAsync(ApplicationShutdownContext context) { - AsyncHelper.RunSync( - () => context.ServiceProvider - .GetRequiredService() - .StopAsync() - ); + await context.ServiceProvider + .GetRequiredService() + .StopAsync(); } } diff --git a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobsModule.cs b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobsModule.cs index aaa4a7ba01..47a165ca83 100644 --- a/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobsModule.cs +++ b/framework/src/Volo.Abp.BackgroundJobs/Volo/Abp/BackgroundJobs/AbpBackgroundJobsModule.cs @@ -1,9 +1,11 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.BackgroundWorkers; using Volo.Abp.DistributedLocking; using Volo.Abp.Guids; using Volo.Abp.Modularity; +using Volo.Abp.Threading; using Volo.Abp.Timing; namespace Volo.Abp.BackgroundJobs; @@ -17,12 +19,21 @@ namespace Volo.Abp.BackgroundJobs; )] public class AbpBackgroundJobsModule : AbpModule { + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + var options = context.ServiceProvider.GetRequiredService>().Value; + if (options.IsJobExecutionEnabled) + { + await context.AddBackgroundWorkerAsync(); + } + } + public override void OnApplicationInitialization(ApplicationInitializationContext context) { var options = context.ServiceProvider.GetRequiredService>().Value; if (options.IsJobExecutionEnabled) { - context.AddBackgroundWorker(); + AsyncHelper.RunSync(() => context.AddBackgroundWorkerAsync()); } } } diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs index 162b2fa472..e1525ade96 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Hangfire/Volo/Abp/BackgroundWorkers/Hangfire/HangfireBackgroundWorkerManager.cs @@ -21,7 +21,7 @@ public class HangfireBackgroundWorkerManager : IBackgroundWorkerManager, ISingle return Task.CompletedTask; } - public void Add(IBackgroundWorker worker) + public async Task AddAsync(IBackgroundWorker worker) { if (worker is IHangfireBackgroundWorker hangfireBackgroundWorker) { diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpBackgroundWorkersQuartzModule.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpBackgroundWorkersQuartzModule.cs index 5884532f1e..07586b461b 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpBackgroundWorkersQuartzModule.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/AbpBackgroundWorkersQuartzModule.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Options; using Volo.Abp.Modularity; using Volo.Abp.Quartz; +using Volo.Abp.Threading; namespace Volo.Abp.BackgroundWorkers.Quartz; @@ -34,6 +35,21 @@ public class AbpBackgroundWorkersQuartzModule : AbpModule } } + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + var quartzBackgroundWorkerOptions = context.ServiceProvider.GetRequiredService>().Value; + if (quartzBackgroundWorkerOptions.IsAutoRegisterEnabled) + { + var backgroundWorkerManager = context.ServiceProvider.GetRequiredService(); + var works = context.ServiceProvider.GetServices().Where(x => x.AutoRegister); + + foreach (var work in works) + { + await backgroundWorkerManager.AddAsync(work); + } + } + } + public override void OnApplicationInitialization(ApplicationInitializationContext context) { var quartzBackgroundWorkerOptions = context.ServiceProvider.GetRequiredService>().Value; @@ -44,7 +60,7 @@ public class AbpBackgroundWorkersQuartzModule : AbpModule foreach (var work in works) { - backgroundWorkerManager.Add(work); + AsyncHelper.RunSync(() => backgroundWorkerManager.AddAsync(work)); } } } diff --git a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs index efee2d2139..954f21bcda 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers.Quartz/Volo/Abp/BackgroundWorkers/Quartz/QuartzBackgroundWorkerManager.cs @@ -33,9 +33,9 @@ public class QuartzBackgroundWorkerManager : IBackgroundWorkerManager, ISingleto } } - public virtual void Add(IBackgroundWorker worker) + public virtual async Task AddAsync(IBackgroundWorker worker) { - AsyncHelper.RunSync(() => ReScheduleJobAsync(worker)); + await ReScheduleJobAsync(worker); } protected virtual async Task ReScheduleJobAsync(IBackgroundWorker worker) diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs index 40c4a70fb1..32341f101e 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/AbpBackgroundWorkersModule.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.Modularity; using Volo.Abp.Threading; @@ -10,6 +11,28 @@ namespace Volo.Abp.BackgroundWorkers; )] public class AbpBackgroundWorkersModule : AbpModule { + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + var options = context.ServiceProvider.GetRequiredService>().Value; + if (options.IsEnabled) + { + await context.ServiceProvider + .GetRequiredService() + .StartAsync(); + } + } + + public async override Task OnApplicationShutdownAsync(ApplicationShutdownContext context) + { + var options = context.ServiceProvider.GetRequiredService>().Value; + if (options.IsEnabled) + { + await context.ServiceProvider + .GetRequiredService() + .StopAsync(); + } + } + public override void OnApplicationInitialization(ApplicationInitializationContext context) { var options = context.ServiceProvider.GetRequiredService>().Value; diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerManager.cs index 62c1f3607b..3276c837c0 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkerManager.cs @@ -26,15 +26,13 @@ public class BackgroundWorkerManager : IBackgroundWorkerManager, ISingletonDepen _backgroundWorkers = new List(); } - public virtual void Add(IBackgroundWorker worker) + public virtual async Task AddAsync(IBackgroundWorker worker) { _backgroundWorkers.Add(worker); if (IsRunning) { - AsyncHelper.RunSync( - () => worker.StartAsync() - ); + await worker.StartAsync(); } } diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs index 5cff67b7de..9df0f6c5e4 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/BackgroundWorkersApplicationInitializationContextExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using JetBrains.Annotations; using Microsoft.Extensions.DependencyInjection; @@ -6,17 +7,17 @@ namespace Volo.Abp.BackgroundWorkers; public static class BackgroundWorkersApplicationInitializationContextExtensions { - public static ApplicationInitializationContext AddBackgroundWorker([NotNull] this ApplicationInitializationContext context) + public async static Task AddBackgroundWorkerAsync([NotNull] this ApplicationInitializationContext context) where TWorker : IBackgroundWorker { Check.NotNull(context, nameof(context)); - context.AddBackgroundWorker(typeof(TWorker)); + await context.AddBackgroundWorkerAsync(typeof(TWorker)); return context; } - public static ApplicationInitializationContext AddBackgroundWorker([NotNull] this ApplicationInitializationContext context, [NotNull] Type workerType) + public async static Task AddBackgroundWorkerAsync([NotNull] this ApplicationInitializationContext context, [NotNull] Type workerType) { Check.NotNull(context, nameof(context)); Check.NotNull(workerType, nameof(workerType)); @@ -26,9 +27,9 @@ public static class BackgroundWorkersApplicationInitializationContextExtensions throw new AbpException($"Given type ({workerType.AssemblyQualifiedName}) must implement the {typeof(IBackgroundWorker).AssemblyQualifiedName} interface, but it doesn't!"); } - context.ServiceProvider + await context.ServiceProvider .GetRequiredService() - .Add( + .AddAsync( (IBackgroundWorker)context.ServiceProvider.GetRequiredService(workerType) ); diff --git a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerManager.cs b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerManager.cs index e1bf0e53f2..7f5268afff 100644 --- a/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerManager.cs +++ b/framework/src/Volo.Abp.BackgroundWorkers/Volo/Abp/BackgroundWorkers/IBackgroundWorkerManager.cs @@ -1,4 +1,5 @@ -using Volo.Abp.Threading; +using System.Threading.Tasks; +using Volo.Abp.Threading; namespace Volo.Abp.BackgroundWorkers; @@ -13,5 +14,5 @@ public interface IBackgroundWorkerManager : IRunnable /// /// The worker. It should be resolved from IOC. /// - void Add(IBackgroundWorker worker); + Task AddAsync(IBackgroundWorker worker); } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs index cb0a32f47c..b4a873dcd4 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; +using System.Threading.Tasks; using Volo.Abp.BackgroundWorkers; using Volo.Abp.DistributedLocking; using Volo.Abp.EventBus.Abstractions; @@ -12,6 +13,7 @@ using Volo.Abp.Json; using Volo.Abp.Modularity; using Volo.Abp.MultiTenancy; using Volo.Abp.Reflection; +using Volo.Abp.Threading; namespace Volo.Abp.EventBus; @@ -30,10 +32,16 @@ public class AbpEventBusModule : AbpModule AddEventHandlers(context.Services); } + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + await context.AddBackgroundWorkerAsync(); + await context.AddBackgroundWorkerAsync(); + } + public override void OnApplicationInitialization(ApplicationInitializationContext context) { - context.AddBackgroundWorker(); - context.AddBackgroundWorker(); + AsyncHelper.RunSync(() => context.AddBackgroundWorkerAsync()); + AsyncHelper.RunSync(() => context.AddBackgroundWorkerAsync()); } private static void AddEventHandlers(IServiceCollection services) diff --git a/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzModule.cs b/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzModule.cs index ade14f1b83..db790a717d 100644 --- a/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzModule.cs +++ b/framework/src/Volo.Abp.Quartz/Volo/Abp/Quartz/AbpQuartzModule.cs @@ -1,8 +1,8 @@ -using Microsoft.Extensions.DependencyInjection; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Quartz; using Quartz.Impl; -using Quartz.Impl.AdoJobStore.Common; using Volo.Abp.Modularity; using Volo.Abp.Threading; @@ -18,8 +18,8 @@ public class AbpQuartzModule : AbpModule context.Services.AddQuartz(options.Properties, build => { - // these are the defaults - if (options.Properties[StdSchedulerFactory.PropertySchedulerJobFactoryType] == null) + // these are the defaults + if (options.Properties[StdSchedulerFactory.PropertySchedulerJobFactoryType] == null) { build.UseMicrosoftDependencyInjectionScopedJobFactory(); } @@ -62,6 +62,23 @@ public class AbpQuartzModule : AbpModule }); } + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + var options = context.ServiceProvider.GetRequiredService>().Value; + + _scheduler = context.ServiceProvider.GetRequiredService(); + + await options.StartSchedulerFactory.Invoke(_scheduler); + } + + public async override Task OnApplicationShutdownAsync(ApplicationShutdownContext context) + { + if (_scheduler.IsStarted) + { + await _scheduler.Shutdown(); + } + } + public override void OnApplicationInitialization(ApplicationInitializationContext context) { var options = context.ServiceProvider.GetRequiredService>().Value; diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreAsyncTestBase_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreAsyncTestBase_Tests.cs new file mode 100644 index 0000000000..5d475966a2 --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreAsyncTestBase_Tests.cs @@ -0,0 +1,62 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Volo.Abp.AspNetCore.TestBase; +using Volo.Abp.Autofac; +using Volo.Abp.Modularity; +using Xunit; + +namespace Volo.Abp.AspNetCore.Mvc; + +public class AbpAspNetCoreAsyncTestBase_Tests : AbpAspNetCoreAsyncTestBase +{ + [Fact] + public async Task Get_API_Response_Test() + { + var result = await GetResponseAsStringAsync("/api"); + result.ShouldBe(await GetRequiredService().GetResponseAsync()); + } +} + +[DependsOn( + typeof(AbpAspNetCoreMvcModule), + typeof(AbpAspNetCoreTestBaseModule), + typeof(AbpAutofacModule) +)] +public class AbpAspNetCoreAsyncTestModule : AbpModule +{ + public override Task ConfigureServicesAsync(ServiceConfigurationContext context) + { + context.Services.AddTransient(); + return Task.CompletedTask; + } + + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + var app = context.GetApplicationBuilder(); + + var dataBaseService = app.ApplicationServices.GetRequiredService(); + + var apiResponse = await dataBaseService.GetResponseAsync(); + app.Map("/api", _ => + { + app.Run(async httpContext => + { + await httpContext.Response.WriteAsync(apiResponse); + }); + }); + + app.UseRouting(); + app.UseConfiguredEndpoints(); + } +} + +public class DataBaseService +{ + public Task GetResponseAsync() + { + return Task.FromResult("hello api!"); + } +} diff --git a/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreAsyncTestBase.cs b/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreAsyncTestBase.cs new file mode 100644 index 0000000000..0d222deab4 --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpAspNetCoreAsyncTestBase.cs @@ -0,0 +1,45 @@ +using System.Globalization; +using System.Net; +using System.Net.Http; +using System.Text.Json; +using System.Threading.Tasks; +using Microsoft.Net.Http.Headers; +using Shouldly; +using Volo.Abp.AspNetCore.TestBase; +using Volo.Abp.Modularity; +using Xunit; + +namespace Volo.Abp.AspNetCore; + +public abstract class AbpAspNetCoreAsyncTestBase : AbpAspNetCoreAsyncIntegratedTestBase, IAsyncLifetime + where TModule : AbpModule +{ + protected virtual async Task GetResponseAsObjectAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + var strResponse = await GetResponseAsStringAsync(url, expectedStatusCode); + return JsonSerializer.Deserialize(strResponse, new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); + } + + protected virtual async Task GetResponseAsStringAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK) + { + using (var response = await GetResponseAsync(url, expectedStatusCode)) + { + return await response.Content.ReadAsStringAsync(); + } + } + + protected virtual async Task GetResponseAsync(string url, HttpStatusCode expectedStatusCode = HttpStatusCode.OK, bool xmlHttpRequest = false) + { + using (var requestMessage = new HttpRequestMessage(HttpMethod.Get, url)) + { + requestMessage.Headers.Add("Accept-Language", CultureInfo.CurrentUICulture.Name); + if (xmlHttpRequest) + { + requestMessage.Headers.Add(HeaderNames.XRequestedWith, "XMLHttpRequest"); + } + var response = await Client.SendAsync(requestMessage); + response.StatusCode.ShouldBe(expectedStatusCode); + return response; + } + } +} diff --git a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/DocsDomainModule.cs b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/DocsDomainModule.cs index ecc52d1f26..d6c7a3a0a6 100644 --- a/modules/docs/src/Volo.Docs.Domain/Volo/Docs/DocsDomainModule.cs +++ b/modules/docs/src/Volo.Docs.Domain/Volo/Docs/DocsDomainModule.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp; @@ -39,7 +40,7 @@ namespace Volo.Docs options.EtoMappings.Add(typeof(DocsDomainModule)); options.EtoMappings.Add(typeof(DocsDomainModule)); }); - + Configure(options => { options.FileSets @@ -65,6 +66,18 @@ namespace Volo.Docs }); } + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) + { + using (var scope = context.ServiceProvider.CreateScope()) + { + if (scope.ServiceProvider.GetRequiredService>().Value.Enable) + { + var documentFullSearch = scope.ServiceProvider.GetRequiredService(); + await documentFullSearch.CreateIndexIfNeededAsync(); + } + } + } + public override void OnApplicationInitialization(ApplicationInitializationContext context) { using (var scope = context.ServiceProvider.CreateScope()) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs index d3663277cb..1f8fb93e4b 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs @@ -1,4 +1,5 @@ -using IdentityServer4.Services; +using System.Threading.Tasks; +using IdentityServer4.Services; using IdentityServer4.Stores; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -133,17 +134,31 @@ public class AbpIdentityServerDomainModule : AbpModule }); } - public override void OnApplicationInitialization(ApplicationInitializationContext context) + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { var options = context.ServiceProvider.GetRequiredService>().Value; if (options.IsCleanupEnabled) { - context.ServiceProvider + await context.ServiceProvider .GetRequiredService() - .Add( + .AddAsync( context.ServiceProvider .GetRequiredService() ); } } + + public override void OnApplicationInitialization(ApplicationInitializationContext context) + { + var options = context.ServiceProvider.GetRequiredService>().Value; + if (options.IsCleanupEnabled) + { + AsyncHelper.RunSync(() => context.ServiceProvider + .GetRequiredService() + .AddAsync( + context.ServiceProvider + .GetRequiredService() + )); + } + } } 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 a64b17a3f9..838c64e004 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 @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -9,7 +10,7 @@ namespace MyCompanyName.MyProjectName.Blazor.Server.Tiered; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG @@ -33,13 +34,13 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - builder.Services.AddApplication(options => + await builder.Services.AddApplicationAsync(options => { options.Services.ReplaceConfiguration(builder.Configuration); }); var app = builder.Build(); - app.InitializeApplication(); - app.Run(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) 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 ef9b6dcf35..b9aad1a4cd 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 @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -9,7 +10,7 @@ namespace MyCompanyName.MyProjectName.Blazor.Server; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG @@ -33,13 +34,13 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - builder.Services.AddApplication(options => + await builder.Services.AddApplicationAsync(options => { options.Services.ReplaceConfiguration(builder.Configuration); }); var app = builder.Build(); - app.InitializeApplication(); - app.Run(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Program.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Program.cs index 1ded753836..12c8d2df84 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor/Program.cs @@ -5,7 +5,7 @@ namespace MyCompanyName.MyProjectName.Blazor; public class Program { - public static async Task Main(string[] args) + public async static Task Main(string[] args) { var builder = WebAssemblyHostBuilder.CreateDefault(args); 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 e6f48c54c2..59b85f1a90 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 @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -9,7 +10,7 @@ namespace MyCompanyName.MyProjectName; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG @@ -33,13 +34,13 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - builder.Services.AddApplication(options => + await builder.Services.AddApplicationAsync(options => { options.Services.ReplaceConfiguration(builder.Configuration); }); var app = builder.Build(); - app.InitializeApplication(); - app.Run(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) 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 e6f48c54c2..59b85f1a90 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 @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -9,7 +10,7 @@ namespace MyCompanyName.MyProjectName; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG @@ -33,13 +34,13 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - builder.Services.AddApplication(options => + await builder.Services.AddApplicationAsync(options => { options.Services.ReplaceConfiguration(builder.Configuration); }); var app = builder.Build(); - app.InitializeApplication(); - app.Run(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) 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 499c2e2b41..aca0769330 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.IdentityServer/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -9,7 +10,7 @@ namespace MyCompanyName.MyProjectName; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG @@ -33,13 +34,13 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - builder.Services.AddApplication(options => + await builder.Services.AddApplicationAsync(options => { options.Services.ReplaceConfiguration(builder.Configuration); }); var app = builder.Build(); - app.InitializeApplication(); - app.Run(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) 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 cdb4a395cc..3cd8f691dc 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 @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -9,7 +10,7 @@ namespace MyCompanyName.MyProjectName.Web; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG @@ -33,13 +34,13 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - builder.Services.AddApplication(options => + await builder.Services.AddApplicationAsync(options => { options.Services.ReplaceConfiguration(builder.Configuration); }); var app = builder.Build(); - app.InitializeApplication(); - app.Run(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) 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 cdb4a395cc..3cd8f691dc 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web/Program.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Web/Program.cs @@ -1,4 +1,5 @@ using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; @@ -9,7 +10,7 @@ namespace MyCompanyName.MyProjectName.Web; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG @@ -33,13 +34,13 @@ public class Program builder.Host.AddAppSettingsSecretsJson() .UseAutofac() .UseSerilog(); - builder.Services.AddApplication(options => + await builder.Services.AddApplicationAsync(options => { options.Services.ReplaceConfiguration(builder.Configuration); }); var app = builder.Build(); - app.InitializeApplication(); - app.Run(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Host/Program.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Host/Program.cs index d5b3c36ff5..39e7bdb524 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Host/Program.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Host/Program.cs @@ -5,7 +5,7 @@ namespace MyCompanyName.MyProjectName.Blazor.Host; public class Program { - public static async Task Main(string[] args) + public async static Task Main(string[] args) { var builder = WebAssemblyHostBuilder.CreateDefault(args); diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/MyProjectNameBlazorHostModule.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/MyProjectNameBlazorHostModule.cs index 951a9e69bc..fdb57b3435 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/MyProjectNameBlazorHostModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/MyProjectNameBlazorHostModule.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Net.Http; +using System.Threading.Tasks; using Blazorise.Bootstrap5; using Blazorise.Icons.FontAwesome; using Microsoft.AspNetCore.Builder; @@ -211,7 +212,7 @@ public class MyProjectNameBlazorHostModule : AbpModule #endif } - public override void OnApplicationInitialization(ApplicationInitializationContext context) + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { var env = context.GetEnvironment(); var app = context.GetApplicationBuilder(); @@ -252,12 +253,9 @@ public class MyProjectNameBlazorHostModule : AbpModule using (var scope = context.ServiceProvider.CreateScope()) { - AsyncHelper.RunSync(async () => - { - await scope.ServiceProvider - .GetRequiredService() - .SeedAsync(); - }); + await scope.ServiceProvider + .GetRequiredService() + .SeedAsync(); } } } 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 1301cbfb3e..c1b5c178d5 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 @@ -1,6 +1,7 @@ using System; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; @@ -9,27 +10,37 @@ namespace MyCompanyName.MyProjectName.Blazor.Server.Host; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG - .MinimumLevel.Debug() + .MinimumLevel.Debug() #else - .MinimumLevel.Information() + .MinimumLevel.Information() #endif - .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Async(c => c.File("Logs/logs.txt")) #if DEBUG - .WriteTo.Async(c => c.Console()) + .WriteTo.Async(c => c.Console()) #endif - .CreateLogger(); + .CreateLogger(); try { Log.Information("Starting web host."); - CreateHostBuilder(args).Build().Run(); + var builder = WebApplication.CreateBuilder(args); + builder.Host.AddAppSettingsSecretsJson() + .UseAutofac() + .UseSerilog(); + await builder.Services.AddApplicationAsync(options => + { + options.Services.ReplaceConfiguration(builder.Configuration); + }); + var app = builder.Build(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) @@ -42,15 +53,4 @@ public class Program Log.CloseAndFlush(); } } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args) - .AddAppSettingsSecretsJson() - .ConfigureWebHostDefaults(webBuilder => - { - - webBuilder.UseStartup(); - }) - .UseAutofac() - .UseSerilog(); } diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Startup.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Startup.cs deleted file mode 100644 index d356f6bede..0000000000 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Blazor.Server.Host/Startup.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; - -namespace MyCompanyName.MyProjectName.Blazor.Server.Host; - -public class Startup -{ - public void ConfigureServices(IServiceCollection services) - { - services.AddApplication(); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) - { - app.InitializeApplication(); - } -} 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 500a2cfc00..328016c3b0 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 @@ -1,7 +1,7 @@ using System; -using System.IO; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; @@ -10,26 +10,37 @@ namespace MyCompanyName.MyProjectName; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG - .MinimumLevel.Debug() + .MinimumLevel.Debug() #else - .MinimumLevel.Information() + .MinimumLevel.Information() #endif - .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Async(c => c.File("Logs/logs.txt")) #if DEBUG - .WriteTo.Async(c => c.Console()) + .WriteTo.Async(c => c.Console()) #endif - .CreateLogger(); + .CreateLogger(); try { Log.Information("Starting web host."); - CreateHostBuilder(args).Build().Run(); + var builder = WebApplication.CreateBuilder(args); + builder.Host.AddAppSettingsSecretsJson() + .UseAutofac() + .UseSerilog(); + await builder.Services.AddApplicationAsync(options => + { + options.Services.ReplaceConfiguration(builder.Configuration); + }); + var app = builder.Build(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) @@ -42,14 +53,4 @@ public class Program Log.CloseAndFlush(); } } - - internal static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .AddAppSettingsSecretsJson() - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }) - .UseAutofac() - .UseSerilog(); } diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Startup.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Startup.cs deleted file mode 100644 index 200da6e542..0000000000 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.HttpApi.Host/Startup.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; - -namespace MyCompanyName.MyProjectName; - -public class Startup -{ - public void ConfigureServices(IServiceCollection services) - { - services.AddApplication(); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) - { - app.InitializeApplication(); - } -} diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs index b6fc071e14..eb854d3bc1 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/MyProjectNameIdentityServerModule.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.DataProtection; @@ -187,7 +188,7 @@ public class MyProjectNameIdentityServerModule : AbpModule #endif } - public override void OnApplicationInitialization(ApplicationInitializationContext context) + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); @@ -227,19 +228,16 @@ public class MyProjectNameIdentityServerModule : AbpModule app.UseAbpSerilogEnrichers(); app.UseConfiguredEndpoints(); - SeedData(context); + await SeedData(context); } - private void SeedData(ApplicationInitializationContext context) + private async Task SeedData(ApplicationInitializationContext context) { - AsyncHelper.RunSync(async () => + using (var scope = context.ServiceProvider.CreateScope()) { - using (var scope = context.ServiceProvider.CreateScope()) - { - await scope.ServiceProvider - .GetRequiredService() - .SeedAsync(); - } - }); + await scope.ServiceProvider + .GetRequiredService() + .SeedAsync(); + } } } 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 500a2cfc00..5f5b29332b 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Program.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Program.cs @@ -1,7 +1,7 @@ using System; -using System.IO; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; @@ -10,26 +10,37 @@ namespace MyCompanyName.MyProjectName; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG - .MinimumLevel.Debug() + .MinimumLevel.Debug() #else - .MinimumLevel.Information() + .MinimumLevel.Information() #endif - .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Async(c => c.File("Logs/logs.txt")) #if DEBUG - .WriteTo.Async(c => c.Console()) + .WriteTo.Async(c => c.Console()) #endif - .CreateLogger(); + .CreateLogger(); try { Log.Information("Starting web host."); - CreateHostBuilder(args).Build().Run(); + var builder = WebApplication.CreateBuilder(args); + builder.Host.AddAppSettingsSecretsJson() + .UseAutofac() + .UseSerilog(); + await builder.Services.AddApplicationAsync(options => + { + options.Services.ReplaceConfiguration(builder.Configuration); + }); + var app = builder.Build(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) @@ -42,14 +53,4 @@ public class Program Log.CloseAndFlush(); } } - - internal static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .AddAppSettingsSecretsJson() - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }) - .UseAutofac() - .UseSerilog(); } diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Startup.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Startup.cs deleted file mode 100644 index 70990fa4da..0000000000 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.IdentityServer/Startup.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; - -namespace MyCompanyName.MyProjectName; - -public class Startup -{ - public void ConfigureServices(IServiceCollection services) - { - services.AddApplication(); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) - { - app.InitializeApplication(); - } -} 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 500a2cfc00..cdeeed1546 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 @@ -1,7 +1,10 @@ using System; using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; @@ -10,26 +13,37 @@ namespace MyCompanyName.MyProjectName; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() #if DEBUG - .MinimumLevel.Debug() + .MinimumLevel.Debug() #else - .MinimumLevel.Information() + .MinimumLevel.Information() #endif - .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Async(c => c.File("Logs/logs.txt")) #if DEBUG - .WriteTo.Async(c => c.Console()) + .WriteTo.Async(c => c.Console()) #endif - .CreateLogger(); + .CreateLogger(); try { Log.Information("Starting web host."); - CreateHostBuilder(args).Build().Run(); + var builder = WebApplication.CreateBuilder(args); + builder.Host.AddAppSettingsSecretsJson() + .UseAutofac() + .UseSerilog(); + await builder.Services.AddApplicationAsync(options => + { + options.Services.ReplaceConfiguration(builder.Configuration); + }); + var app = builder.Build(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) @@ -42,14 +56,4 @@ public class Program Log.CloseAndFlush(); } } - - internal static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .AddAppSettingsSecretsJson() - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }) - .UseAutofac() - .UseSerilog(); } diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Startup.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Startup.cs deleted file mode 100644 index 265fb2e7d7..0000000000 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Host/Startup.cs +++ /dev/null @@ -1,18 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; - -namespace MyCompanyName.MyProjectName; - -public class Startup -{ - public void ConfigureServices(IServiceCollection services) - { - services.AddApplication(); - } - - public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) - { - app.InitializeApplication(); - } -} diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/MyProjectNameWebUnifiedModule.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/MyProjectNameWebUnifiedModule.cs index ed4f5d13e9..49b63a407d 100644 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/MyProjectNameWebUnifiedModule.cs +++ b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/MyProjectNameWebUnifiedModule.cs @@ -1,5 +1,6 @@ using System.IO; using System.Reflection; +using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; @@ -140,7 +141,7 @@ public class MyProjectNameWebUnifiedModule : AbpModule #endif } - public override void OnApplicationInitialization(ApplicationInitializationContext context) + public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context) { var app = context.GetApplicationBuilder(); var env = context.GetEnvironment(); @@ -180,12 +181,9 @@ public class MyProjectNameWebUnifiedModule : AbpModule using (var scope = context.ServiceProvider.CreateScope()) { - AsyncHelper.RunSync(async () => - { - await scope.ServiceProvider - .GetRequiredService() - .SeedAsync(); - }); + await scope.ServiceProvider + .GetRequiredService() + .SeedAsync(); } } } 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 6b523328cc..a8443ddaca 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 @@ -1,7 +1,10 @@ using System; using System.IO; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; @@ -10,22 +13,37 @@ namespace MyCompanyName.MyProjectName; public class Program { - public static int Main(string[] args) + public async static Task Main(string[] args) { Log.Logger = new LoggerConfiguration() +#if DEBUG .MinimumLevel.Debug() +#else + .MinimumLevel.Information() +#endif .MinimumLevel.Override("Microsoft", LogEventLevel.Information) + .MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) .Enrich.FromLogContext() .WriteTo.Async(c => c.File("Logs/logs.txt")) #if DEBUG - .WriteTo.Async(c => c.Console()) + .WriteTo.Async(c => c.Console()) #endif - .CreateLogger(); + .CreateLogger(); try { Log.Information("Starting web host."); - CreateHostBuilder(args).Build().Run(); + var builder = WebApplication.CreateBuilder(args); + builder.Host.AddAppSettingsSecretsJson() + .UseAutofac() + .UseSerilog(); + await builder.Services.AddApplicationAsync(options => + { + options.Services.ReplaceConfiguration(builder.Configuration); + }); + var app = builder.Build(); + await app.InitializeApplicationAsync(); + await app.RunAsync(); return 0; } catch (Exception ex) @@ -38,14 +56,4 @@ public class Program Log.CloseAndFlush(); } } - - internal static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .AddAppSettingsSecretsJson() - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }) - .UseAutofac() - .UseSerilog(); } diff --git a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Startup.cs b/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Startup.cs deleted file mode 100644 index 2be66923ce..0000000000 --- a/templates/module/aspnet-core/host/MyCompanyName.MyProjectName.Web.Unified/Startup.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; - -namespace MyCompanyName.MyProjectName; - -public class Startup -{ - public void ConfigureServices(IServiceCollection services) - { - services.AddApplication(); - } - - public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory) - { - app.InitializeApplication(); - } -}