检查另一个项目的IHostedService的状态吗?
我目前正在研究一个项目,该项目通过托管服务背景任务处理文件,将这些文件持续到数据库,然后通过网页向用户显示数据。网页和背景任务在不同的.NET项目中:Web API中的背景任务和MVC应用中的网页。任务每5分钟运行一次。
我的目标是能够从MVC控制器接触到托管服务,并询问任务是否正在运行或是否不运行。我不确定我缺少托管服务中的某些东西,还是第三方库是答案。我也不确定是否有一种直接的,程序化的属性来执行此操作,或者必须是某种消息系统。我在研究中看到了兔子和hang鱼之类的东西,但是我不确定是否有必要。
以下代码全部来自托管服务项目。这是文档处理器的代码,
public class DocProcessor: IHostedService
{
private readonly ILogger<DocProcessor> _logger;
private readonly IWorker _worker;
public DocProcessor(ILogger<DocProcessor> logger, IWorker worker)
{
_logger = logger;
_worker = worker;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await _worker.DoWork(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping task.");
return Task.CompletedTask;
}
}
这里是iWorker界面:
public interface IWorker
{
Task DoWork(CancellationToken cancellationToken);
}
这是docprocessorworker:
public class DocProcessorWorker: IWorker
{
private readonly ILogger<DocProcessorWorker> _logger;
public DocProcessorWorker(ILogger<DocProcessorWorker> logger)
{
_logger = logger;
}
public async Task DoWork(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
... Work Being Done
await Task.Delay(TimeSpan.FromMinutes(5), cancellationToken);
}
}
}
以及program.cs的服务部分:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHostedService<DocProcessor>();
builder.Services.AddSingleton<IWorker, DocProcessorWorker>();
...Other stuff...
var app = builder.Build();
I'm currently working on a project which processes files via a Hosted Service background task, persists those files to a DB, then show the user that data via a webpage. The webpage and the background task are in different .NET projects: the background task in a Web API and the webpage in a MVC App. The task runs every 5 minutes.
My goal is to be able to, from the MVC Controller, reach out to the Hosted Service and ask if the task is running or if it isn't. I'm unsure if there's something within Hosted Services that I'm missing or if a 3rd-party library is the answer. I'm also unsure if there's a direct, programmatic property way to do this or if it'll have to be some sort of messaging system. I've seen things like Rabbit and Hangfire in my research, but I'm not sure if that's necessary.
The following code is all from the Hosted Service project. Here is the code for the Document Processor
public class DocProcessor: IHostedService
{
private readonly ILogger<DocProcessor> _logger;
private readonly IWorker _worker;
public DocProcessor(ILogger<DocProcessor> logger, IWorker worker)
{
_logger = logger;
_worker = worker;
}
public async Task StartAsync(CancellationToken cancellationToken)
{
await _worker.DoWork(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Stopping task.");
return Task.CompletedTask;
}
}
Here is the IWorker interface:
public interface IWorker
{
Task DoWork(CancellationToken cancellationToken);
}
Here is the DocProcessorWorker:
public class DocProcessorWorker: IWorker
{
private readonly ILogger<DocProcessorWorker> _logger;
public DocProcessorWorker(ILogger<DocProcessorWorker> logger)
{
_logger = logger;
}
public async Task DoWork(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
... Work Being Done
await Task.Delay(TimeSpan.FromMinutes(5), cancellationToken);
}
}
}
And the services portion of Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddHostedService<DocProcessor>();
builder.Services.AddSingleton<IWorker, DocProcessorWorker>();
...Other stuff...
var app = builder.Build();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论