Skip to content

Commit

Permalink
Merge pull request #10928 from abpframework/new-hosting-model
Browse files Browse the repository at this point in the history
Use new hosting model for the app template.
  • Loading branch information
hikalkan authored Dec 25, 2021
2 parents 0438a6a + d9a5b86 commit 8fce683
Show file tree
Hide file tree
Showing 90 changed files with 1,519 additions and 635 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting;

public static class AbpWebAssemblyHostBuilderExtensions
{
public static async Task<IAbpApplicationWithExternalServiceProvider> AddApplicationAsync<TStartupModule>(
[NotNull] this WebAssemblyHostBuilder builder,
Action<AbpWebAssemblyApplicationCreationOptions> options)
where TStartupModule : IAbpModule
{
Check.NotNull(builder, nameof(builder));

// Related this commit(https://github.com/dotnet/aspnetcore/commit/b99d805bc037fcac56afb79abeb7d5a43141c85e)
// Microsoft.AspNetCore.Blazor.BuildTools has been removed in net 5.0.
// This call may be removed when we find a suitable solution.
// System.Runtime.CompilerServices.AsyncStateMachineAttribute
Castle.DynamicProxy.Generators.AttributesToAvoidReplicating.Add<AsyncStateMachineAttribute>();

builder.Services.AddSingleton<IConfiguration>(builder.Configuration);
builder.Services.AddSingleton(builder);

var application = await builder.Services.AddApplicationAsync<TStartupModule>(opts =>
{
options?.Invoke(new AbpWebAssemblyApplicationCreationOptions(builder, opts));
});

return application;
}

public static IAbpApplicationWithExternalServiceProvider AddApplication<TStartupModule>(
[NotNull] this WebAssemblyHostBuilder builder,
Action<AbpWebAssemblyApplicationCreationOptions> options)
Expand Down Expand Up @@ -51,7 +75,7 @@ public static async Task InitializeAsync(
((ComponentsClientScopeServiceProviderAccessor)serviceProvider
.GetRequiredService<IClientScopeServiceProviderAccessor>()).ServiceProvider = serviceProvider;

application.Initialize(serviceProvider);
await application.InitializeAsync(serviceProvider);
await InitializeModulesAsync(serviceProvider);
await SetCurrentLanguageAsync(serviceProvider);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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<TModule>
where TModule : IAbpModule
{
protected WebApplication WebApplication { get; set; }

protected TestServer Server { get; set; }

protected HttpClient Client { get; set; }

protected IServiceProvider ServiceProvider { get; set; }

protected virtual T GetService<T>()
{
return ServiceProvider.GetService<T>();
}

protected virtual T GetRequiredService<T>()
{
return ServiceProvider.GetRequiredService<T>();
}

public virtual async Task InitializeAsync()
{
var builder = WebApplication.CreateBuilder();
builder.Host.ConfigureServices(services =>
{
services.AddSingleton<IHostLifetime, TestNoopHostLifetime>();
services.AddSingleton<IServer, TestServer>();
})
.UseAutofac();

await builder.Services.AddApplicationAsync<TModule>(options =>
{
options.Services.ReplaceConfiguration(builder.Configuration);
});

await ConfigureServicesAsync(builder.Services);
WebApplication = builder.Build();
await WebApplication.InitializeApplicationAsync();
await WebApplication.StartAsync();

Server = WebApplication.Services.GetRequiredService<IHost>().GetTestServer();
Client = Server.CreateClient();

ServiceProvider = Server.Services;
ServiceProvider.GetRequiredService<ITestServerAccessor>().Server = Server;
}

public virtual async Task DisposeAsync()
{
await WebApplication.DisposeAsync();
}

protected virtual Task ConfigureServicesAsync(IServiceCollection services)
{
return Task.CompletedTask;
}

#region GetUrl

/// <summary>
/// Gets default URL for given controller type.
/// </summary>
/// <typeparam name="TController">The type of the controller.</typeparam>
protected virtual string GetUrl<TController>()
{
return "/" + typeof(TController).Name.RemovePostFix("Controller", "AppService", "ApplicationService", "Service");
}

/// <summary>
/// Gets default URL for given controller type's given action.
/// </summary>
/// <typeparam name="TController">The type of the controller.</typeparam>
protected virtual string GetUrl<TController>(string actionName)
{
return GetUrl<TController>() + "/" + actionName;
}

/// <summary>
/// Gets default URL for given controller type's given action with query string parameters (as anonymous object).
/// </summary>
/// <typeparam name="TController">The type of the controller.</typeparam>
protected virtual string GetUrl<TController>(string actionName, object queryStringParamsAsAnonymousObject)
{
var url = GetUrl<TController>(actionName);

var dictionary = new RouteValueDictionary(queryStringParamsAsAnonymousObject);
if (dictionary.Any())
{
url += "?" + dictionary.Select(d => $"{d.Key}={d.Value}").JoinAsString("&");
}

return url;
}

#endregion
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public abstract class AbpAspNetCoreIntegratedTestBase<TStartup> : AbpTestBaseWit

protected HttpClient Client { get; }

protected override IServiceProvider ServiceProvider { get; }

private readonly IHost _host;

protected AbpAspNetCoreIntegratedTestBase()
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.RequestLocalization;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -11,13 +12,35 @@
using Volo.Abp.AspNetCore.Tracing;
using Volo.Abp.AspNetCore.Uow;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Threading;

namespace Microsoft.AspNetCore.Builder;

public static class AbpApplicationBuilderExtensions
{
private const string ExceptionHandlingMiddlewareMarker = "_AbpExceptionHandlingMiddleware_Added";

public async static Task InitializeApplicationAsync([NotNull] this IApplicationBuilder app)
{
Check.NotNull(app, nameof(app));

app.ApplicationServices.GetRequiredService<ObjectAccessor<IApplicationBuilder>>().Value = app;
var application = app.ApplicationServices.GetRequiredService<IAbpApplicationWithExternalServiceProvider>();
var applicationLifetime = app.ApplicationServices.GetRequiredService<IHostApplicationLifetime>();

applicationLifetime.ApplicationStopping.Register(() =>
{
AsyncHelper.RunSync(() => application.ShutdownAsync());
});

applicationLifetime.ApplicationStopped.Register(() =>
{
application.Dispose();
});

await application.InitializeAsync(app.ApplicationServices);
}

public static void InitializeApplication([NotNull] this IApplicationBuilder app)
{
Check.NotNull(app, nameof(app));
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,31 +18,37 @@ public override void ConfigureServices(ServiceConfigurationContext context)
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<IJobQueueManager>()
.StartAsync()
);
await context.ServiceProvider
.GetRequiredService<IJobQueueManager>()
.StartAsync();
}

private static void StopJobQueueManager(ApplicationShutdownContext context)
private async static Task StopJobQueueManagerAsync(ApplicationShutdownContext context)
{
AsyncHelper.RunSync(
() => context.ServiceProvider
.GetRequiredService<IJobQueueManager>()
.StopAsync()
);
await context.ServiceProvider
.GetRequiredService<IJobQueueManager>()
.StopAsync();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,12 +19,21 @@ namespace Volo.Abp.BackgroundJobs;
)]
public class AbpBackgroundJobsModule : AbpModule
{
public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpBackgroundJobOptions>>().Value;
if (options.IsJobExecutionEnabled)
{
await context.AddBackgroundWorkerAsync<IBackgroundJobWorker>();
}
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var options = context.ServiceProvider.GetRequiredService<IOptions<AbpBackgroundJobOptions>>().Value;
if (options.IsJobExecutionEnabled)
{
context.AddBackgroundWorker<IBackgroundJobWorker>();
AsyncHelper.RunSync(() => context.AddBackgroundWorkerAsync<IBackgroundJobWorker>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public Task StopAsync(CancellationToken cancellationToken = default)
return Task.CompletedTask;
}

public void Add(IBackgroundWorker worker)
public async Task AddAsync(IBackgroundWorker worker)
{
if (worker is IHangfireBackgroundWorker hangfireBackgroundWorker)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Options;
using Volo.Abp.Modularity;
using Volo.Abp.Quartz;
using Volo.Abp.Threading;

namespace Volo.Abp.BackgroundWorkers.Quartz;

Expand Down Expand Up @@ -34,6 +35,21 @@ public override void OnPreApplicationInitialization(ApplicationInitializationCon
}
}

public async override Task OnApplicationInitializationAsync(ApplicationInitializationContext context)
{
var quartzBackgroundWorkerOptions = context.ServiceProvider.GetRequiredService<IOptions<AbpBackgroundWorkerQuartzOptions>>().Value;
if (quartzBackgroundWorkerOptions.IsAutoRegisterEnabled)
{
var backgroundWorkerManager = context.ServiceProvider.GetRequiredService<IBackgroundWorkerManager>();
var works = context.ServiceProvider.GetServices<IQuartzBackgroundWorker>().Where(x => x.AutoRegister);

foreach (var work in works)
{
await backgroundWorkerManager.AddAsync(work);
}
}
}

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{
var quartzBackgroundWorkerOptions = context.ServiceProvider.GetRequiredService<IOptions<AbpBackgroundWorkerQuartzOptions>>().Value;
Expand All @@ -44,7 +60,7 @@ public override void OnApplicationInitialization(ApplicationInitializationContex

foreach (var work in works)
{
backgroundWorkerManager.Add(work);
AsyncHelper.RunSync(() => backgroundWorkerManager.AddAsync(work));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public virtual async Task StopAsync(CancellationToken cancellationToken = defaul
}
}

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)
Expand Down
Loading

0 comments on commit 8fce683

Please sign in to comment.