ASP .NET Core &-}部分背景服务
此篇文章浏览了来自这个博客平台的实际背景服务 <:}文件监视器 与Polly retroll Policy politics ,\ channel\ MISK9Q}基于电子邮件的队列,配有断路机@,_语系搜索索引和 hash=-基底更改探测器>~,* 及更多{.
一. 导言导言 导言 导言 导言
内 部分#1我们在ASP-.NET Core.中探索了实施背景服务的基本方法。 IHostedService, BackgroundService“,” 生命周期管理“,” 以及关机处理的常见陷阱“MS K2”
@'}现在到了在行动中看到这些模式的时间 =. 在文章里 =, we') 会从制作博客平台上查看真实的背景服务 =%, 演示_:*
- 将标记下载文件同步到数据库的系统监视器
- Polly 重试策略的频道-基email队列
- 批量背景要求的分析事件发送器
- 具有 hash-}基于更改检测功能的语义搜索索引器
- 定期验证外部 URL的断断链接检查器
- 受扶养事务部门间的启动协调
- 范围界定和《框架》生命周期管理
每个例子都说明在建立生产背景服务时你遇到的共同问题的实际解决办法*
带有 Polly Restry 的 1:文件系统监视器
第一种服务 we'll 检查将查看标记博客日志的目录, 当它们更改时自动处理这些日志 .}这是事件=-驱动的背景处理_.}的一个完美例子
问题
当创建或修改标记文件时@ : @
- 读取文件内容
- 分析标记下调元数据@( title @,}分类=%,公布日期_)
- 输入 HTML 并提取纯文本
- 保存到数据库
- 触发翻译成其他语言
- 语义搜索索引
挑战 #:# FileSystemWatcher 正在写入文件时事件会着火@ , @ cause IOException 当您试图读取它时 @ . @ @
解决方案@: @Markdown 董事观察服务
public class MarkdownDirectoryWatcherService(
MarkdownConfig markdownConfig,
IServiceScopeFactory serviceScopeFactory,
IStartupCoordinator startupCoordinator,
ILogger<MarkdownDirectoryWatcherService> logger)
: IHostedService
{
private FileSystemWatcher _fileSystemWatcher;
private Task _awaitChangeTask = Task.CompletedTask;
public Task StartAsync(CancellationToken cancellationToken)
{
_fileSystemWatcher = new FileSystemWatcher
{
Path = markdownConfig.MarkdownPath,
NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite |
NotifyFilters.CreationTime | NotifyFilters.Size,
Filter = "*.md",
IncludeSubdirectories = true
};
_fileSystemWatcher.EnableRaisingEvents = true;
// Start background processing in a separate task
_awaitChangeTask = Task.Run(() => AwaitChanges(cancellationToken), cancellationToken);
logger.LogInformation("Started watching directory {Directory}", markdownConfig.MarkdownPath);
// Signal ready - watcher is set up and listening
startupCoordinator.SignalReady(StartupServiceNames.MarkdownDirectoryWatcher);
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
// Proper cleanup
_fileSystemWatcher.EnableRaisingEvents = false;
_fileSystemWatcher.Dispose();
logger.LogInformation("Stopped watching directory");
return Task.CompletedTask;
}
private async Task AwaitChanges(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
var fileEvent = _fileSystemWatcher.WaitForChanged(WatcherChangeTypes.All);
if (fileEvent.ChangeType == WatcherChangeTypes.Changed ||
fileEvent.ChangeType == WatcherChangeTypes.Created)
{
await OnChangedAsync(fileEvent);
}
else if (fileEvent.ChangeType == WatcherChangeTypes.Deleted)
{
await OnDeletedAsync(fileEvent);
}
else if (fileEvent.ChangeType == WatcherChangeTypes.Renamed)
{
await OnRenamedAsync(fileEvent);
}
}
}
}
注意几个密钥模式@: @
- Isadd Servicice , 而非背景服务 我们需要精细控制监视器的设置
- 单独背景任务 # -#
StartAsync返回即时@,}处理发生于AwaitChanges - 启动协调 “-”信号,当它准备好接受辅助服务时
- 适当处置 - 禁用并处置文件系统监视器
StopAsync
处理 Polly 文件锁定问题
最有趣的部分是我们如何处理仍在写入的文件@. @ 波利 是一个“.”NET抗御力库,提供重试政策@,断路器 @,和更多#:
private async Task OnChangedAsync(WaitForChangedResult e)
{
if (e.Name == null) return;
// Serilog activity for distributed tracing
using var activity = Log.Logger.StartActivity("Markdown File Changed {Name}", e.Name);
// Define a retry policy for file access issues
var retryPolicy = Policy
.Handle<IOException>() // Only handle IO exceptions (like file in use)
.WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromMilliseconds(500 * retryAttempt),
(exception, timeSpan, retryCount, context) =>
{
activity?.Activity?.SetTag("Retry Attempt", retryCount);
logger.LogWarning(
"File is in use, retrying attempt {RetryCount} after {TimeSpan}",
retryCount, timeSpan);
});
try
{
var fileName = e.Name;
var isTranslated = Path.GetFileNameWithoutExtension(e.Name).Contains(".");
var language = MarkdownBaseService.EnglishLanguage;
var directory = markdownConfig.MarkdownPath;
if (isTranslated)
{
language = Path.GetFileNameWithoutExtension(e.Name).Split('.').Last();
fileName = Path.GetFileName(fileName);
directory = markdownConfig.MarkdownTranslatedPath;
}
var filePath = Path.Combine(directory, fileName);
using var scope = serviceScopeFactory.CreateScope();
var blogService = scope.ServiceProvider.GetRequiredService<IBlogService>();
// Use the Polly retry policy
await retryPolicy.ExecuteAsync(async () =>
{
// Read the file - might throw IOException if locked
var markdown = await File.ReadAllTextAsync(filePath);
var slug = Path.GetFileNameWithoutExtension(fileName);
if (isTranslated)
{
slug = slug.Split('.').First();
}
// Save to database
var savedModel = await blogService.SavePost(slug, language, markdown);
activity?.Activity?.SetTag("Page Processed", savedModel.Slug);
// Index in semantic search (only for main directory files)
if (!e.Name.Contains(Path.DirectorySeparatorChar))
{
await IndexPostForSemanticSearchAsync(scope, savedModel, language);
}
// Trigger translation for English posts
if (language == MarkdownBaseService.EnglishLanguage &&
!string.IsNullOrEmpty(savedModel.Markdown))
{
var translateService = scope.ServiceProvider
.GetRequiredService<IBackgroundTranslateService>();
await translateService.TranslateForAllLanguages(
new PageTranslationModel
{
OriginalFileName = filePath,
OriginalMarkdown = savedModel.Markdown,
Persist = true
});
}
});
activity?.Complete();
}
catch (Exception exception)
{
activity?.Complete(LogEventLevel.Error, exception);
}
}
这里的密钥模式 @ :
- 投票重试政策 @- @Addintial 后退@(500 @ms,}#1 @s,}{1.5sMS K6(2_sQ},} @sMS K9*s%s)}♪
- 只重试 IO 例外 其它例外
- 范围服务 - 创建每个文件的覆盖范围以避免存在周期问题
- 结构性伐木 使用“-” 血清 追踪活动的活动
- 连锁业务 -}保存 @→}索引 *→}翻譯
重试策略处理常有的情况, 当更改事件火灾时文本编辑器仍然在写入文件@.
视觉流动
graph TD
A[File Modified] --> B[FileSystemWatcher Event]
B --> C{File Locked?}
C -->|Yes| D[Wait 500ms * Attempt]
D --> E{Retry < 5?}
E -->|Yes| C
E -->|No| F[Log Error]
C -->|No| G[Read File]
G --> H[Parse Markdown]
H --> I[Save to Database]
I --> J{Main Directory?}
J -->|Yes| K[Index for Search]
J -->|No| L{English?}
K --> L
L -->|Yes| M[Trigger Translation]
L -->|No| N[Complete]
M --> N
style A stroke:#059669,stroke-width:3px,color:#10b981
style I stroke:#2563eb,stroke-width:3px,color:#3b82f6
style K stroke:#7c3aed,stroke-width:3px,color:#8b5cf6
style M stroke:#d97706,stroke-width:3px,color:#f59e0b
登记在 Program.cs (所有托管服务都遵循此模式 @):
builder.Services.AddHostedService<MarkdownDirectoryWatcherService>();
Q2:通道@-基于电子邮件队列的示例
电子邮件发送是背景服务的一个典型使用选项@. @ You donñ'}您在谈判 SMTP,}时不想屏蔽 HTTP 请求, 所以您将邮件排队并发送到后台 @.}
问题
当用户提交备注或联系表@: @% 1
- 审定和保存提交书
- 设置电子邮件通知
- SMTP=)}返回响应 即刻回覆 @(@don_'#t块
- 以重试逻辑在背景中发送邮件
- 优雅地处理 SMTP 失败问题 @ ( @ 断路器@ MS K1 @
解答@:电子邮件服务
此服务使用 a Channel<T> 用于排队和 Polly 的恢复能力@: @%
public class EmailSenderHostedService : IEmailSenderHostedService
{
private readonly Channel<BaseEmailModel> _mailMessages =
Channel.CreateUnbounded<BaseEmailModel>();
private readonly CancellationTokenSource _cancellationTokenSource = new();
private Task _sendTask = Task.CompletedTask;
private readonly IEmailService _emailService;
private readonly ILogger<EmailSenderHostedService> _logger;
private readonly IAsyncPolicy _policyWrap;
public EmailSenderHostedService(
IEmailService emailService,
ILogger<EmailSenderHostedService> logger)
{
_emailService = emailService;
_logger = logger;
// Retry policy: 3 attempts with exponential backoff
var retryPolicy = Policy
.Handle<SmtpException>()
.WaitAndRetryAsync(3,
attempt => TimeSpan.FromSeconds(2 * attempt),
(exception, timeSpan, retryCount, context) =>
{
logger.LogWarning(exception,
"Retry {RetryCount} for sending email failed", retryCount);
});
// Circuit breaker: open after 5 failures, stay open for 1 minute
var circuitBreakerPolicy = Policy
.Handle<SmtpException>()
.CircuitBreakerAsync(
5,
TimeSpan.FromMinutes(1),
onBreak: (exception, timespan) =>
{
logger.LogError(
"Circuit broken due to too many failures. Breaking for {BreakDuration}",
timespan);
},
onReset: () =>
{
logger.LogInformation("Circuit reset. Resuming email delivery.");
},
onHalfOpen: () =>
{
logger.LogInformation("Circuit in half-open state. Testing connection...");
});
// Combine retry and circuit breaker
_policyWrap = Policy.WrapAsync(retryPolicy, circuitBreakerPolicy);
}
public async Task SendEmailAsync(BaseEmailModel message)
{
await _mailMessages.Writer.WriteAsync(message);
}
public Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting background e-mail delivery");
_sendTask = DeliverAsync(_cancellationTokenSource.Token);
return Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping background e-mail delivery");
// Proper shutdown sequence
await _cancellationTokenSource.CancelAsync();
_mailMessages.Writer.Complete(); // Critical: complete the channel
// Wait for the background task to finish
await Task.WhenAny(_sendTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
private async Task DeliverAsync(CancellationToken token)
{
_logger.LogInformation("E-mail background delivery started");
try
{
// Process items as they arrive
while (await _mailMessages.Reader.WaitToReadAsync(token))
{
BaseEmailModel? message = null;
try
{
message = await _mailMessages.Reader.ReadAsync(token);
// Execute with retry policy and circuit breaker
await _policyWrap.ExecuteAsync(async () =>
{
switch (message)
{
case ContactEmailModel contactEmailModel:
await _emailService.SendContactEmail(contactEmailModel);
break;
case CommentEmailModel commentEmailModel:
await _emailService.SendCommentEmail(commentEmailModel);
break;
case ConfirmEmailModel confirmEmailModel:
await _emailService.SendConfirmationEmail(confirmEmailModel);
break;
}
});
_logger.LogInformation("Email from {SenderEmail} sent", message.SenderEmail);
}
catch (OperationCanceledException)
{
break; // Shutdown requested
}
catch (Exception exc)
{
_logger.LogError(exc,
"Couldn't send an e-mail from {SenderEmail}",
message?.SenderEmail);
}
}
}
catch (OperationCanceledException)
{
_logger.LogWarning("E-mail background delivery canceled");
}
_logger.LogInformation("E-mail background delivery stopped");
}
public void Dispose()
{
_cancellationTokenSource.Dispose();
}
}
显示的密钥模式@:
- 频道作为队列 -无限制的电文频道
- 政策总结 - 重试在断路器内
- 优利关闭 @ -_ 完成频道@ MS K1} 取消牌称@ MPK2 等待任务
- 模式匹配 @ - 切换不同的电子邮件类型
- 涵盖寿命期 - 以單身身份建立 MSC1 保持長度- 永存狀態
重试和电路断路器可视化
graph TD
A[Queue Email] --> B[Write to Channel]
B --> C[Background Task Reads]
C --> D{Send Email}
D -->|Success| E[Log Success]
D -->|SmtpException| F{Retry Count < 3?}
F -->|Yes| G[Wait 2s * Attempt]
G --> D
F -->|No| H{Circuit Breaker}
H -->|< 5 Failures| I[Log Failure]
H -->|5+ Failures| J[Open Circuit]
J --> K[Wait 1 Minute]
K --> L[Half-Open: Test]
L -->|Success| M[Close Circuit]
L -->|Failure| J
E --> N[Continue]
I --> N
style A stroke:#059669,stroke-width:3px,color:#10b981
style D stroke:#2563eb,stroke-width:3px,color:#3b82f6
style J stroke:#dc2626,stroke-width:3px,color:#ef4444
style M stroke:#059669,stroke-width:3px,color:#10b981
这展示出一种生产型号: - 级别模式: MPK1
- 重试政策 临时网络问题@)
- 断路器 @(_#如果 SMTP 正在下降@,}唐尼•'t 继续敲击它 )
- 频道 提供自然的后压 @ ( @ {如果我们能 MS K1} t send,} 信件队列上@ )} *
分析事件发件人 @(_Umami})
此服务性队列分析事件, 并发送到 木美 分析服务器@. @ it'}类似电子邮件服务,但更简单一点#— @No try policy
public class UmamiBackgroundSender(
IServiceScopeFactory scopeFactory,
ILogger<UmamiBackgroundSender> logger) : IHostedService
{
private readonly CancellationTokenSource _cancellationTokenSource = new();
private readonly Channel<SendBackgroundPayload> _channel =
Channel.CreateUnbounded<SendBackgroundPayload>();
private Task _sendTask = Task.CompletedTask;
public Task StartAsync(CancellationToken cancellationToken)
{
_sendTask = SendRequest(_cancellationTokenSource.Token);
return Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
logger.LogInformation("UmamiBackgroundSender is stopping.");
// Standard shutdown pattern
await _cancellationTokenSource.CancelAsync();
_channel.Writer.Complete();
try
{
await Task.WhenAny(_sendTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
catch (OperationCanceledException)
{
logger.LogWarning("StopAsync operation was canceled.");
}
}
public async Task TrackPageView(string url, string title, UmamiPayload? payload = null)
{
await using var scope = scopeFactory.CreateAsyncScope();
var payloadService = scope.ServiceProvider.GetRequiredService<PayloadService>();
var sendPayload = payloadService.PopulateFromPayload(payload, null);
sendPayload.Url = url;
sendPayload.Title = title;
await _channel.Writer.WriteAsync(new SendBackgroundPayload("event", sendPayload));
logger.LogInformation("Umami pageview event queued");
}
private async Task SendRequest(CancellationToken token)
{
logger.LogInformation("Umami background delivery started");
// Double while loop: outer waits for items, inner drains all available
while (await _channel.Reader.WaitToReadAsync(token))
{
while (_channel.Reader.TryRead(out var payload))
{
try
{
using var scope = scopeFactory.CreateScope();
var client = scope.ServiceProvider.GetRequiredService<UmamiClient>();
await client.Send(payload.Payload, type: payload.EventType);
logger.LogInformation("Umami background event sent: {EventType}",
payload.EventType);
}
catch (OperationCanceledException)
{
logger.LogWarning("Umami background delivery canceled.");
return;
}
catch (Exception ex)
{
logger.LogError(ex, "Error sending Umami background event.");
}
}
}
}
private record SendBackgroundPayload(string EventType, UmamiPayload Payload);
}
电子邮件服务的密钥差异@:
- 无复原力政策 -分析器是 @'}如果它失败了,我们只要登录和继续
- 双环循环时双圈 - 内环流排出所有可用项目以提高效率
- 范围服务创建 - 每個發送者都為 HTTP 客戶端提供新的範圍
这表明并非所有的背景服务都需要复杂的错误处理@. @ for non -}“临界遥测”_,简单记录也许就足够了 @.}
与背景服务有关的定期背景工作
现在让我们来看看那些定期工作的服务, 而不是处理队列@ . BrokenLinkCheckerBackgroundService 校对:Soup
public class BrokenLinkCheckerBackgroundService : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<BrokenLinkCheckerBackgroundService> _logger;
private readonly HttpClient _httpClient;
private readonly TimeSpan _checkInterval = TimeSpan.FromHours(1);
private const int BatchSize = 20;
public BrokenLinkCheckerBackgroundService(
IServiceProvider serviceProvider,
ILogger<BrokenLinkCheckerBackgroundService> logger,
IHttpClientFactory httpClientFactory)
{
_serviceProvider = serviceProvider;
_logger = logger;
_httpClient = httpClientFactory.CreateClient("BrokenLinkChecker");
_httpClient.Timeout = TimeSpan.FromSeconds(30);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("Broken Link Checker Background Service started");
// Initial delay to let the application fully start
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
while (!stoppingToken.IsCancellationRequested)
{
try
{
await CheckLinksAsync(stoppingToken);
await FetchArchiveUrlsAsync(stoppingToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error in Broken Link Checker Background Service");
}
_logger.LogInformation("Broken Link Checker sleeping for {Interval}", _checkInterval);
await Task.Delay(_checkInterval, stoppingToken);
}
_logger.LogInformation("Broken Link Checker Background Service stopped");
}
private async Task CheckLinksAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Starting link validity check");
using var scope = _serviceProvider.CreateScope();
var brokenLinkService = scope.ServiceProvider.GetRequiredService<IBrokenLinkService>();
var linksToCheck = await brokenLinkService.GetLinksToCheckAsync(BatchSize, cancellationToken);
_logger.LogInformation("Found {Count} links to check", linksToCheck.Count);
foreach (var link in linksToCheck)
{
if (cancellationToken.IsCancellationRequested) break;
try
{
var (statusCode, isBroken, error) = await CheckUrlAsync(link.OriginalUrl, cancellationToken);
await brokenLinkService.UpdateLinkStatusAsync(
link.Id, statusCode, isBroken, error, cancellationToken);
if (isBroken)
{
_logger.LogWarning("Link is broken: {Url} (Status: {StatusCode})",
link.OriginalUrl, statusCode);
}
// Be respectful to servers
await Task.Delay(TimeSpan.FromSeconds(2), cancellationToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error checking link: {Url}", link.OriginalUrl);
await brokenLinkService.UpdateLinkStatusAsync(
link.Id, 0, true, ex.Message, cancellationToken);
}
}
}
private async Task<(int statusCode, bool isBroken, string? error)> CheckUrlAsync(
string url,
CancellationToken cancellationToken)
{
try
{
using var request = new HttpRequestMessage(HttpMethod.Head, url);
request.Headers.UserAgent.ParseAdd(
"Mozilla/5.0 (compatible; MostlylucidBot/1.0; +https://www.mostlylucid.net)");
using var response = await _httpClient.SendAsync(
request,
HttpCompletionOption.ResponseHeadersRead,
cancellationToken);
var statusCode = (int)response.StatusCode;
var isBroken = response.StatusCode == HttpStatusCode.NotFound ||
response.StatusCode == HttpStatusCode.Gone ||
statusCode >= 500;
return (statusCode, isBroken, null);
}
catch (HttpRequestException ex)
{
return (0, true, ex.Message);
}
catch (TaskCanceledException ex) when (ex.InnerException is TimeoutException)
{
return (0, true, "Request timed out");
}
}
}
显示模式@: @%
- 背景服务基级 “-”适合定期工作。
- 初始延迟 @ -_ 等待其它服务到初始端
- 条纹 - process {20} 在一个时间中连接,} 不是一次全部
- 限制利率 检查之间第二次延误以保持礼貌
- 范围服务创建 - 每个批量的新范围
- 取消代号检查 * -* 如果请求关闭, 请提前休息 。
5: 语义搜索索引及 HashMS K1基于变化检测的示例
语义搜索索引器更精密@— @it 只显示已实际更改的#-}index posts
public class SemanticIndexingBackgroundService : BackgroundService
{
private readonly ILogger<SemanticIndexingBackgroundService> _logger;
private readonly IServiceProvider _serviceProvider;
private readonly ISemanticSearchService _semanticSearchService;
private readonly MarkdownConfig _markdownConfig;
private readonly SemanticSearchConfig _semanticSearchConfig;
private readonly TimeSpan _indexInterval = TimeSpan.FromHours(1);
private readonly TimeSpan _startupDelay = TimeSpan.FromSeconds(30);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
if (!_semanticSearchConfig.Enabled)
{
_logger.LogInformation("Semantic search is disabled, indexing service will not run");
return;
}
_logger.LogInformation("Semantic indexing background service starting...");
// Wait for other services to initialise
await Task.Delay(_startupDelay, stoppingToken);
// Initialise the semantic search service
try
{
await _semanticSearchService.InitializeAsync(stoppingToken);
_logger.LogInformation("Semantic search initialized successfully");
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to initialize semantic search service");
return;
}
// Initial indexing
await IndexAllMarkdownFilesAsync(stoppingToken);
// Periodic re-indexing to catch any changes
while (!stoppingToken.IsCancellationRequested)
{
try
{
await Task.Delay(_indexInterval, stoppingToken);
await IndexAllMarkdownFilesAsync(stoppingToken);
}
catch (OperationCanceledException)
{
break;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error during periodic indexing");
}
}
_logger.LogInformation("Semantic indexing background service stopped");
}
private async Task IndexAllMarkdownFilesAsync(CancellationToken stoppingToken)
{
var markdownPath = _markdownConfig.MarkdownPath;
if (!Directory.Exists(markdownPath))
{
_logger.LogWarning("Markdown directory does not exist: {Path}", markdownPath);
return;
}
// Get ONLY files in the main directory, NOT subdirectories
var markdownFiles = Directory.GetFiles(markdownPath, "*.md", SearchOption.TopDirectoryOnly);
_logger.LogInformation("Found {Count} markdown files to index in {Path}",
markdownFiles.Length, markdownPath);
var indexedCount = 0;
var skippedCount = 0;
var errorCount = 0;
using var scope = _serviceProvider.CreateScope();
var markdownRenderingService = scope.ServiceProvider
.GetRequiredService<MarkdownRenderingService>();
foreach (var filePath in markdownFiles)
{
if (stoppingToken.IsCancellationRequested)
break;
try
{
var result = await IndexMarkdownFileAsync(
filePath,
markdownRenderingService,
stoppingToken);
if (result == IndexResult.Indexed)
indexedCount++;
else if (result == IndexResult.Skipped)
skippedCount++;
}
catch (Exception ex)
{
errorCount++;
_logger.LogError(ex, "Error indexing file: {FilePath}", filePath);
}
// Delay to avoid overwhelming the embedding service
await Task.Delay(100, stoppingToken);
}
_logger.LogInformation(
"Indexing complete: {Indexed} indexed, {Skipped} skipped (unchanged), {Errors} errors",
indexedCount, skippedCount, errorCount);
}
private async Task<IndexResult> IndexMarkdownFileAsync(
string filePath,
MarkdownRenderingService markdownRenderingService,
CancellationToken stoppingToken)
{
var fileName = Path.GetFileNameWithoutExtension(filePath);
// Skip translated files (they have language suffix like .es.md, .fr.md)
if (fileName.Contains('.'))
{
var parts = fileName.Split('.');
if (parts.Length >= 2 && parts[^1].Length == 2)
{
return IndexResult.Skipped;
}
}
var markdown = await File.ReadAllTextAsync(filePath, stoppingToken);
var fileInfo = new FileInfo(filePath);
var blogPost = markdownRenderingService.GetPageFromMarkdown(
markdown,
fileInfo.LastWriteTimeUtc,
filePath);
if (blogPost.IsHidden)
{
_logger.LogDebug("Skipping hidden post: {Slug}", blogPost.Slug);
return IndexResult.Skipped;
}
// Compute content hash
var contentHash = ComputeContentHash(blogPost.PlainTextContent);
// Check if reindexing is needed
var needsReindex = await _semanticSearchService.NeedsReindexingAsync(
blogPost.Slug,
MarkdownBaseService.EnglishLanguage,
contentHash,
stoppingToken);
if (!needsReindex)
{
_logger.LogDebug("Skipping unchanged post: {Slug}", blogPost.Slug);
return IndexResult.Skipped;
}
// Create document for indexing
var document = new BlogPostDocument
{
Id = $"{blogPost.Slug}_{MarkdownBaseService.EnglishLanguage}",
Slug = blogPost.Slug,
Title = blogPost.Title,
Content = blogPost.PlainTextContent,
Language = MarkdownBaseService.EnglishLanguage,
Categories = blogPost.Categories.ToList(),
PublishedDate = blogPost.PublishedDate,
ContentHash = contentHash
};
await _semanticSearchService.IndexPostAsync(document, stoppingToken);
_logger.LogInformation("Indexed post: {Slug}", blogPost.Slug);
return IndexResult.Indexed;
}
private static string ComputeContentHash(string content)
{
using var sha256 = SHA256.Create();
var bytes = Encoding.UTF8.GetBytes(content);
var hashBytes = sha256.ComputeHash(bytes);
return Convert.ToBase64String(hashBytes);
}
private enum IndexResult
{
Indexed,
Skipped
}
}
密钥模式@: @%
- 設定 & M- @% 驱动 只有当语义搜索被启用时, -才运行
- 基于 Hash- 的改变检测 @ -_ SHAQ256}内容的散列 *,}跳过, 如果未变的话
- 限制利率 @ - @ @ I100 @% 1} 索引操作之间的延迟
- 过滤处理 - 跳过已翻译的文件和隐藏的日志
- 详细指标 - 音轨索引@/skipped @/error 计数
启动协调例@6:
最后一个例子展示了服务如何协调它们的启动@. @The MarkdownReAddPostsService 在启动时, - k处理所有标记文件@ ( useful after schema change @ MS K2} but signs when it='\ 准备就绪, 这样其他服务就能知道他们可以继续 < MSC4\ {
public class MarkdownReAddPostsService(
MarkdownConfig markdownConfig,
IServiceScopeFactory serviceScopeFactory,
IStartupCoordinator startupCoordinator,
ILogger<MarkdownReAddPostsService> logger) : IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
{
if (!markdownConfig.ReAddPosts)
{
logger.LogInformation("ReAddPosts is disabled, skipping bulk post re-add");
// Signal ready immediately since we're not doing anything
startupCoordinator.SignalReady(StartupServiceNames.MarkdownReAddPosts);
return;
}
logger.LogWarning("ReAddPosts is ENABLED - re-processing ALL markdown posts on startup");
// Run in background so it doesn't block startup
_ = Task.Run(async () =>
{
try
{
// Small delay to let other services initialize
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
await ReAddAllPostsAsync(cancellationToken);
}
finally
{
// Signal ready when complete (even if there were errors)
startupCoordinator.SignalReady(StartupServiceNames.MarkdownReAddPosts);
}
}, cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
private async Task ReAddAllPostsAsync(CancellationToken cancellationToken)
{
using var activity = Log.Logger.StartActivity("ReAdd All Posts");
var stopwatch = Stopwatch.StartNew();
try
{
using var scope = serviceScopeFactory.CreateScope();
var blogService = scope.ServiceProvider.GetRequiredService<IBlogService>();
var markdownBlogService = scope.ServiceProvider.GetRequiredService<IMarkdownBlogService>();
var semanticSearchService = scope.ServiceProvider.GetService<ISemanticSearchService>();
// Process English files
var markdownFiles = Directory.GetFiles(
markdownConfig.MarkdownPath,
"*.md",
SearchOption.TopDirectoryOnly);
logger.LogInformation("Found {Count} English markdown files to process",
markdownFiles.Length);
int processed = 0;
int failed = 0;
foreach (var filePath in markdownFiles)
{
if (cancellationToken.IsCancellationRequested) break;
var slug = Path.GetFileNameWithoutExtension(filePath);
try
{
var blogModel = await markdownBlogService.GetPage(filePath);
blogModel.Language = MarkdownBaseService.EnglishLanguage;
await blogService.SavePost(blogModel);
// Index in semantic search
if (semanticSearchService != null)
{
await IndexPostAsync(semanticSearchService, blogModel,
MarkdownBaseService.EnglishLanguage);
}
processed++;
if (processed % 10 == 0)
{
logger.LogInformation("Processed {Count}/{Total} English posts",
processed, markdownFiles.Length);
}
}
catch (Exception ex)
{
failed++;
logger.LogError(ex, "Failed to process {Slug}", slug);
}
}
// Process translated files
var translatedFiles = Directory.GetFiles(
markdownConfig.MarkdownTranslatedPath,
"*.md",
SearchOption.TopDirectoryOnly);
logger.LogInformation("Found {Count} translated markdown files to process",
translatedFiles.Length);
int translatedProcessed = 0;
int translatedFailed = 0;
foreach (var filePath in translatedFiles)
{
if (cancellationToken.IsCancellationRequested) break;
var fileNameWithoutExt = Path.GetFileNameWithoutExtension(filePath);
if (!fileNameWithoutExt.Contains(".")) continue;
var parts = fileNameWithoutExt.Split('.');
var slug = parts[0];
var language = parts[^1];
try
{
var blogModel = await markdownBlogService.GetPage(filePath);
blogModel.Language = language;
await blogService.SavePost(blogModel);
if (semanticSearchService != null)
{
await IndexPostAsync(semanticSearchService, blogModel, language);
}
translatedProcessed++;
if (translatedProcessed % 50 == 0)
{
logger.LogInformation("Processed {Count}/{Total} translated posts",
translatedProcessed, translatedFiles.Length);
}
}
catch (Exception ex)
{
translatedFailed++;
logger.LogError(ex, "Failed to process translated post {Slug} ({Language})",
slug, language);
}
}
stopwatch.Stop();
activity?.Activity?.SetTag("English Processed", processed);
activity?.Activity?.SetTag("English Failed", failed);
activity?.Activity?.SetTag("Translated Processed", translatedProcessed);
activity?.Activity?.SetTag("Translated Failed", translatedFailed);
activity?.Activity?.SetTag("Duration", stopwatch.Elapsed.ToString());
activity?.Complete();
logger.LogWarning(
"ReAddPosts completed in {Duration}. English: {EnglishProcessed}/{EnglishTotal}, Translated: {TranslatedProcessed}/{TranslatedTotal}",
stopwatch.Elapsed,
processed, markdownFiles.Length,
translatedProcessed, translatedFiles.Length);
}
catch (Exception ex)
{
activity?.Complete(LogEventLevel.Error, ex);
logger.LogError(ex, "Error during ReAddPosts");
}
}
}
密钥模式@: @%
- 启动协调 备好辅助服务时的-信号
- 纵火,终于忘记 -THIG.Run,有最后的区块保证信号
- 进度记录 @- log every {10}英语日志@,}每个#50}翻译文章
- 结构性活动 -Serilog活动,带有可观测标签
- Two- 阶段处理 @ -_ 英文文件先@ MS K1} 然后再翻译文件
协调流动
graph TD
A[App Starts] --> B[MarkdownReAddPostsService.StartAsync]
B --> C{ReAddPosts Enabled?}
C -->|No| D[Signal Ready Immediately]
C -->|Yes| E[Start Background Task]
E --> F[Wait 5 Seconds]
F --> G[Process All English Posts]
G --> H[Process All Translated Posts]
H --> I[Signal Ready in Finally]
D --> J[Other Services Can Start]
I --> J
J --> K[MarkdownDirectoryWatcher Starts]
J --> L[SemanticIndexer Starts]
style A stroke:#059669,stroke-width:3px,color:#10b981
style I stroke:#2563eb,stroke-width:3px,color:#3b82f6
style J stroke:#7c3aed,stroke-width:3px,color:#8b5cf6
您将注意到上述用途的每个示例@ ' @ label IServiceScopeFactory 创建范围@ . @ this is'_ t可选}—it @'{对于与实体框架和其他范围服务合作来说是必需的\ MS K4} Let'}*s dig into why.#
背景事务和范围界定
背景服务最棘手的一个方面是管理整个服务期 — 实体框架核心.这里 MSSK1你需要知道什么?.
问题:單身與範圍
登记在 AddHostedService<T>() 是 单吨数.它们创建了一次并活过整个应用周期@.但您希望使用的许多服务是: 范围范围d:
DbContext@- 被默认范围所控制\ ( 并且有很好的原因>#)IHttpContextAccessor符合HTTP要求的范围- 登记在
AddScoped<T>()
如果您试图将范围服务输入单人@ , @ 您\ MS K1} 这将获得此例外@ MPK2
Cannot consume scoped service 'YourDbContext' from singleton 'YourBackgroundService'
DbCtext 为何被限定
实体框架_'%s DbContext 跟踪实体并管理数据库连接@. @ it'_%s 设计为缩略语@ -# living}:{
// BAD: DbContext lives forever, accumulates tracked entities,
// connections may timeout, change tracking becomes stale
public class BrokenService : BackgroundService
{
private readonly MyDbContext _dbContext; // Injected once, lives forever
public BrokenService(MyDbContext dbContext)
{
_dbContext = dbContext; // This context will never be disposed!
}
}
与长“-”有问题的活 DbContext:
- 内存泄漏 跟踪实体积累
- 存储数据 -\ change tracker does @'}% t 可以看到外部变化
- 连接问题 - 连接可能超时或再循环
- 合并货币错误 -DbContext is @' t线@-安全
解决方案@: @ Iservices CopePaterFatorial
本篇文章中的每一个例子都使用相同的模式 — 为每个工作单位设定一个范围 :
public class CorrectService : BackgroundService
{
private readonly IServiceScopeFactory _scopeFactory;
public CorrectService(IServiceScopeFactory scopeFactory)
{
_scopeFactory = scopeFactory; // This is a singleton, safe to inject
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// Create a new scope for each iteration
using var scope = _scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
// DbContext is fresh, will be disposed when scope ends
await ProcessWorkAsync(dbContext, stoppingToken);
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
}
// Scope disposed here - DbContext disposed, connections returned to pool
}
}
范围 颗粒
您应该多久创建一个新的范围@ ? @ 它取决于您的工作量 @ :} @
循环迭代 最常见的):
while (!stoppingToken.IsCancellationRequested)
{
using var scope = _scopeFactory.CreateScope();
// Process batch...
}
每个项目 处理队列时 @( @% 1}
await foreach (var item in channel.Reader.ReadAllAsync(stoppingToken))
{
using var scope = _scopeFactory.CreateScope();
// Process single item...
}
每个批次 当您需要交易语义学时, (@ MS K1 @
var items = await GetBatchAsync(100);
using var scope = _scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
await using var transaction = await dbContext.Database.BeginTransactionAsync();
foreach (var item in items)
{
// Process within same transaction...
}
await transaction.CommitAsync();
同步范围
对于 Async 操作来说 @,_}更喜欢 CreateAsyncScope() 正确处理同步处置@: @%
await using var scope = _scopeFactory.CreateAsyncScope();
var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
await dbContext.SaveChangesAsync(stoppingToken);
// Scope disposed asynchronously
常见错误
“1.”在封闭区获取覆盖范围服务“:”
// BAD: dbContext captured, will outlive its scope
var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
_ = Task.Run(async () =>
{
await Task.Delay(10000);
await dbContext.SaveChangesAsync(); // Scope already disposed!
});
2. 重新使用横跨 Async 边界的空域:
// BAD: Scope should match unit of work
using var scope = _scopeFactory.CreateScope();
while (!stoppingToken.IsCancellationRequested)
{
var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
await ProcessAsync(dbContext); // Same stale context every iteration
await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken);
}
@3. 忘了处置@:
// BAD: Scope never disposed, DbContext leaks
var scope = _scopeFactory.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<MyDbContext>();
await ProcessAsync(dbContext);
// Where's the dispose?
总是使用 using 或声明 await using 同步范围@. @% 1
摘要+: 在所有示例中的模式
透过这些真实的#-world services,}呈现出几种模式
“1.” 适当的关闭处理
每个服务都遵循同样的关闭模式 :
public async Task StopAsync(CancellationToken cancellationToken)
{
// 1. Cancel the cancellation token
await _cancellationTokenSource.CancelAsync();
// 2. Complete any channels
_channel.Writer.Complete();
// 3. Wait for background tasks with timeout
await Task.WhenAny(_backgroundTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
2. 建立服务范围
背景服务为单子@ , @ 但他们通常需要范围化的服务 *\ MS K1}\ 类似 DbContext).} 每个示例都创建了范围=:}
using var scope = _serviceProvider.CreateScope();
var scopedService = scope.ServiceProvider.GetRequiredService<IScopedService>();
*3. 复原力模式
不同服务使用不同的抗御战略
- Polly 重试 -File system watcher @(Handles 锁定文件@)
- Polly 断路器 -电子邮件发件人+(防止SMTP敲锤_)
- 限制利率 - 链接检查器和索引器 @( 对外部服务表示礼貌@)
- 基于 Hash- 的改变检测 -语义索引器 @(#aa 避免昂贵 re-indexing@)}
4. 观察能力
每项服务包括全面的伐木服务*:
- 启动@ / @ shuttown 事件 - 总是登录
- 进度衡量指标 -批处理日志计数
- 血清活动 用于分布式追踪
- 以上下文记录错误 有足够信息来诊断问题
@ 5._ 配置@ MS K1_ 驱动行为
服务检查配置@: @%
if (!_config.Enabled)
{
_logger.LogInformation("Service is disabled");
return;
}
这将允许您启用无代码更改的\ / 残疾特性@ MS K1\ {
结论结论
这些真实的“-”世界性实例表明, ASP+.NET核心的背景服务可以处理复杂的假想 ‘:’}
- 事件=-}驱动处理 带有文件系统监视器
- 基于 Quue @ - @% 的处理 有频道
- 定期任务 具有背景服务
- 弹性业务 与波兰政策
- 协调启动 海关协调处
- 高效率的变革检测 含有内容散列
最重要的课目: :
- 总是在 StopAsync 中完成频道并取消标记
- 创建范围扩展服务的范围
- 为您的假想选择正确的弹性模式
- 全面可观测日志
- 优雅的例外
- 当服务互相依赖时使用启动协调
使用这些模式,您可以建立具有抗御力的“,”、可观测性强且易于维护的“.”等级背景服务。