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

View File

@ -34,14 +34,21 @@ public static class BlogApplicationBuilderExtension
builder.Services.AddSingleton<EssayScanService>();
builder.Services.AddSingleton<RendererService>();
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,
Action<WebApplicationBuilder> configureWebApplicationBuilder,
Action<WebApplication> configureWebApplication)
{
builder.Services.AddHostedService<WebApplicationHostedService>(provider =>
new WebApplicationHostedService(configureWebApplicationBuilder,
configureWebApplication, provider));
builder.WebApplicationConfigurations.Add(configureWebApplication);
}
}

View File

@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using YaeBlog.Core.Abstractions;
using YaeBlog.Core.Builder;
using YaeBlog.Core.Processors;
using YaeBlog.Core.Services;
namespace YaeBlog.Core.Extensions;
@ -9,7 +10,7 @@ public static class BlogApplicationExtension
{
internal static void ConfigureDefaultBlogApplication(this BlogApplication application)
{
//application.UsePostRenderProcessor<ImagePostRenderProcessor>();
}
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 Action<WebApplication> _configureWebApplication;
private readonly List<Action<WebApplication>> _webApplicationConfigurations;
private Website? _currentWebsite;
public WebApplicationHostedService(Action<WebApplicationBuilder> configureWebApplicationBuilder,
Action<WebApplication> configureWebApplication,
public WebApplicationHostedService(List<Action<WebApplicationBuilder>> webApplicationBuilderConfigurations,
List<Action<WebApplication>> webApplicationConfigurations,
IServiceProvider hostServiceProvider)
{
_configureWebApplication = configureWebApplication;
configureWebApplicationBuilder(_websiteBuilder);
_webApplicationConfigurations = webApplicationConfigurations;
foreach (Action<WebApplicationBuilder> configure in webApplicationBuilderConfigurations)
{
configure(_websiteBuilder);
}
AddHostServices(hostServiceProvider);
}
@ -31,7 +35,10 @@ public class WebApplicationHostedService : IHostedService
}
WebApplication application = _websiteBuilder.Build();
_configureWebApplication(application);
foreach (Action<WebApplication> configure in _webApplicationConfigurations)
{
configure(application);
}
IHostLifetime websiteLifetime = application.Services.GetRequiredService<IHostLifetime>();
_currentWebsite = new Website(application, websiteLifetime);
}

View File

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