YaeBlog/YaeBlog.Core/Services/WebApplicationHostedService.cs

112 lines
3.2 KiB
C#
Raw Normal View History

2024-01-20 12:26:30 +08:00
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2024-01-20 17:09:01 +08:00
using Microsoft.Extensions.Options;
using YaeBlog.Core.Models;
2024-01-20 12:26:30 +08:00
namespace YaeBlog.Core.Services;
public class WebApplicationHostedService : IHostedService
{
private readonly WebApplicationBuilder _websiteBuilder = WebApplication.CreateBuilder();
private readonly List<Action<WebApplication>> _webApplicationConfigurations;
2024-01-20 12:26:30 +08:00
private Website? _currentWebsite;
public WebApplicationHostedService(List<Action<WebApplicationBuilder>> webApplicationBuilderConfigurations,
List<Action<WebApplication>> webApplicationConfigurations,
2024-01-20 12:26:30 +08:00
IServiceProvider hostServiceProvider)
{
_webApplicationConfigurations = webApplicationConfigurations;
foreach (Action<WebApplicationBuilder> configure in webApplicationBuilderConfigurations)
{
configure(_websiteBuilder);
}
2024-01-20 12:26:30 +08:00
AddHostServices(hostServiceProvider);
}
public async Task BuildWebsite()
{
if (_currentWebsite is not null)
{
await _currentWebsite.ShutdownAsync(new CancellationToken());
}
WebApplication application = _websiteBuilder.Build();
foreach (Action<WebApplication> configure in _webApplicationConfigurations)
{
configure(application);
}
2024-01-20 12:26:30 +08:00
IHostLifetime websiteLifetime = application.Services.GetRequiredService<IHostLifetime>();
_currentWebsite = new Website(application, websiteLifetime);
}
public Task RunAsync()
{
if (_currentWebsite is not null)
{
return _currentWebsite.RunAsync();
}
throw new InvalidOperationException("Website has not been built.");
}
public Task ShutdownAsync()
{
if (_currentWebsite is { Running: true })
{
return _currentWebsite.ShutdownAsync(new CancellationToken());
}
throw new InvalidOperationException("Website is not running.");
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await BuildWebsite();
_ = RunAsync();
}
public Task StopAsync(CancellationToken cancellationToken)
{
if (_currentWebsite is { Running: true })
{
return _currentWebsite.ShutdownAsync(cancellationToken);
}
return Task.CompletedTask;
}
private void AddHostServices(IServiceProvider provider)
{
_websiteBuilder.Services.AddSingleton<EssayContentService>(_ =>
provider.GetRequiredService<EssayContentService>());
2024-01-20 17:09:01 +08:00
_websiteBuilder.Services.AddTransient<BlogOptions>(_ =>
provider.GetRequiredService<IOptions<BlogOptions>>().Value);
2024-01-20 12:26:30 +08:00
}
private class Website(WebApplication application, IHostLifetime websiteLifetime)
{
public bool Running { get; private set; }
public Task RunAsync()
{
Running = true;
return application.RunAsync();
}
public async Task ShutdownAsync(CancellationToken cancellationToken)
{
if (!Running)
{
await websiteLifetime.StopAsync(cancellationToken);
}
Running = false;
}
}
}