refactor: 支持设置多个WebApplicationBuilder和WebApplication配置函数

This commit is contained in:
jackfiled 2024-01-25 15:55:09 +08:00
parent 0f58e4ce4b
commit 154ea2b7f2
5 changed files with 33 additions and 12 deletions

View File

@ -1,4 +1,5 @@
using Markdig; using Markdig;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.Metrics; using Microsoft.Extensions.Diagnostics.Metrics;
@ -13,6 +14,10 @@ public sealed class BlogApplicationBuilder : IHostApplicationBuilder
{ {
private readonly HostApplicationBuilder _hostApplicationBuilder; private readonly HostApplicationBuilder _hostApplicationBuilder;
internal List<Action<WebApplicationBuilder>> WebApplicationBuilderConfigurations { get; } = [];
internal List<Action<WebApplication>> WebApplicationConfigurations { get; } = [];
public MarkdownPipelineBuilder MarkdigPipelineBuilder { get; } public MarkdownPipelineBuilder MarkdigPipelineBuilder { get; }
public DeserializerBuilder YamlDeserializerBuilder { get; } public DeserializerBuilder YamlDeserializerBuilder { get; }

View File

@ -34,14 +34,21 @@ public static class BlogApplicationBuilderExtension
builder.Services.AddSingleton<EssayScanService>(); builder.Services.AddSingleton<EssayScanService>();
builder.Services.AddSingleton<RendererService>(); builder.Services.AddSingleton<RendererService>();
builder.Services.AddSingleton<EssayContentService>(); builder.Services.AddSingleton<EssayContentService>();
builder.Services.AddHostedService<WebApplicationHostedService>((provider)
=> new WebApplicationHostedService(builder.WebApplicationBuilderConfigurations,
builder.WebApplicationConfigurations, provider));
}
public static void ConfigureWebApplicationBuilder(this BlogApplicationBuilder builder,
Action<WebApplicationBuilder> configureWebApplicationBuilder)
{
builder.WebApplicationBuilderConfigurations.Add(configureWebApplicationBuilder);
} }
public static void ConfigureWebApplication(this BlogApplicationBuilder builder, public static void ConfigureWebApplication(this BlogApplicationBuilder builder,
Action<WebApplicationBuilder> configureWebApplicationBuilder,
Action<WebApplication> configureWebApplication) Action<WebApplication> configureWebApplication)
{ {
builder.Services.AddHostedService<WebApplicationHostedService>(provider => builder.WebApplicationConfigurations.Add(configureWebApplication);
new WebApplicationHostedService(configureWebApplicationBuilder,
configureWebApplication, provider));
} }
} }

View File

@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using YaeBlog.Core.Abstractions; using YaeBlog.Core.Abstractions;
using YaeBlog.Core.Builder; using YaeBlog.Core.Builder;
using YaeBlog.Core.Processors;
using YaeBlog.Core.Services; using YaeBlog.Core.Services;
namespace YaeBlog.Core.Extensions; namespace YaeBlog.Core.Extensions;
@ -9,7 +10,7 @@ public static class BlogApplicationExtension
{ {
internal static void ConfigureDefaultBlogApplication(this BlogApplication application) internal static void ConfigureDefaultBlogApplication(this BlogApplication application)
{ {
//application.UsePostRenderProcessor<ImagePostRenderProcessor>();
} }
public static void UsePreRenderProcessor<T>(this BlogApplication application) public static void UsePreRenderProcessor<T>(this BlogApplication application)

View File

@ -10,16 +10,20 @@ public class WebApplicationHostedService : IHostedService
{ {
private readonly WebApplicationBuilder _websiteBuilder = WebApplication.CreateBuilder(); private readonly WebApplicationBuilder _websiteBuilder = WebApplication.CreateBuilder();
private readonly Action<WebApplication> _configureWebApplication; private readonly List<Action<WebApplication>> _webApplicationConfigurations;
private Website? _currentWebsite; private Website? _currentWebsite;
public WebApplicationHostedService(Action<WebApplicationBuilder> configureWebApplicationBuilder, public WebApplicationHostedService(List<Action<WebApplicationBuilder>> webApplicationBuilderConfigurations,
Action<WebApplication> configureWebApplication, List<Action<WebApplication>> webApplicationConfigurations,
IServiceProvider hostServiceProvider) IServiceProvider hostServiceProvider)
{ {
_configureWebApplication = configureWebApplication; _webApplicationConfigurations = webApplicationConfigurations;
configureWebApplicationBuilder(_websiteBuilder); foreach (Action<WebApplicationBuilder> configure in webApplicationBuilderConfigurations)
{
configure(_websiteBuilder);
}
AddHostServices(hostServiceProvider); AddHostServices(hostServiceProvider);
} }
@ -31,7 +35,10 @@ public class WebApplicationHostedService : IHostedService
} }
WebApplication application = _websiteBuilder.Build(); WebApplication application = _websiteBuilder.Build();
_configureWebApplication(application); foreach (Action<WebApplication> configure in _webApplicationConfigurations)
{
configure(application);
}
IHostLifetime websiteLifetime = application.Services.GetRequiredService<IHostLifetime>(); IHostLifetime websiteLifetime = application.Services.GetRequiredService<IHostLifetime>();
_currentWebsite = new Website(application, websiteLifetime); _currentWebsite = new Website(application, websiteLifetime);
} }

View File

@ -10,7 +10,8 @@ public static class BlogApplicationBuilderExtensions
{ {
public static void UseFluentTheme(this BlogApplicationBuilder builder) public static void UseFluentTheme(this BlogApplicationBuilder builder)
{ {
builder.ConfigureWebApplication(ConfigureWebApplicationBuilder, ConfigureWebApplication); builder.ConfigureWebApplicationBuilder(ConfigureWebApplicationBuilder);
builder.ConfigureWebApplication(ConfigureWebApplication);
} }
private static void ConfigureWebApplicationBuilder(WebApplicationBuilder builder) private static void ConfigureWebApplicationBuilder(WebApplicationBuilder builder)