gpt4 book ai didi

c# - 如何创建在每个给定时间段运行一个函数的 BackGround 服务?使用 C#(asp.net 核心 3.1.1)

转载 作者:行者123 更新时间:2023-12-04 15:32:56 25 4
gpt4 key购买 nike

我正在尝试每隔指定的时间间隔调用一个函数,对于使用后台服务的那个 m,这是我所做的:
这是我具有该功能的警报 Controller :

public class AlertingController : ControllerBase
{
private readonly DatabaseContext _context;
private readonly IMapper _mapper;

public AlertingController(DatabaseContext context, IMapper mapper)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
public AlertingController()
{

}
//function that adds in the DB
public async Task<AlertingResponse> GetAlertingToDB()
{
AlertingResponse dataGet;

using (var httpClient = new HttpClient())
{
using (var response = await httpClient
.GetAsync(MetricApiLink))
{
string apiResponse = await response.Content.ReadAsStringAsync();
dataGet = JsonConvert.DeserializeObject<AlertingResponse>(apiResponse);
}
}
if (dataGet.data.alerts != null || dataGet.data.alerts.Count > 0)
{
foreach (var alert in dataGet.data.alerts)
{
CreateAlertQuery QueryAlert = new CreateAlertQuery();
QueryAlert.Name = alert.labels.alertname;
QueryAlert.Instance = alert.labels.instance;
QueryAlert.Serverity = alert.labels.severity;
QueryAlert.Summary = alert.annotations.summary;
QueryAlert.State = alert.state;
QueryAlert.ActiveAt = alert.activeAt;
var _Alert = _mapper.Map<AlertingDataModel>(QueryAlert);
_context.Alertings.Add(_Alert);
await _context.SaveChangesAsync();
}
}

return null;
}
}

我已经使用 HTTPGET 请求测试了该方法,它工作正常,将警报添加到我的数据库中:

enter image description here

我创建了一个服务,我在其中调用了函数 GetAlertingToDB() :
    internal interface IScopedAlertingService
{
Task DoWork(CancellationToken stoppingToken);
}
public class ScopedAlertingService : IScopedAlertingService
{
private int executionCount = 0;
private readonly ILogger _logger;

public ScopedAlertingService(ILogger<ScopedAlertingService> logger)
{
_logger = logger;
}

public async Task DoWork(CancellationToken stoppingToken)
{
AlertingController _AlertingToDB = new AlertingController();
while (!stoppingToken.IsCancellationRequested)
{
executionCount++;

_logger.LogInformation(
"Scoped Processing Service is working. Count: {Count}", executionCount);

await _AlertingToDB.GetAlertingToDB();

await Task.Delay(10000, stoppingToken);
}
}
}

我还创建了将使用我的服务的类,并将在后台运行:
public class ConsumeScopedServiceHostedService : BackgroundService
{
private readonly ILogger<ConsumeScopedServiceHostedService> _logger;

public ConsumeScopedServiceHostedService(IServiceProvider services,
ILogger<ConsumeScopedServiceHostedService> logger)
{
Services = services;
_logger = logger;
}

public IServiceProvider Services { get; }

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation(
"Consume Scoped Service Hosted Service running.");

await DoWork(stoppingToken);
}

private async Task DoWork(CancellationToken stoppingToken)
{
_logger.LogInformation(
"Consume Scoped Service Hosted Service is working.");

using (var scope = Services.CreateScope())
{
var scopedProcessingService =
scope.ServiceProvider
.GetRequiredService<IScopedAlertingService>();

await scopedProcessingService.DoWork(stoppingToken);
}
}

public override async Task StopAsync(CancellationToken stoppingToken)
{
_logger.LogInformation(
"Consume Scoped Service Hosted Service is stopping.");

await Task.CompletedTask;
}
}

我在启动类上注入(inject)了依赖项并添加了托管服务:
        services.AddHostedService<ConsumeScopedServiceHostedService>();
services.AddScoped<IScopedAlertingService, ScopedAlertingService>();

这些功能运行良好,直到调用 GetAlertingToDB()功能,它不起作用。

任何帮助都会很棒,谢谢大家:)

最佳答案

我个人会重新安排您的解决方案,以便您的后台服务不需要创建 Controller .相反, Controller ,如果您仍然需要它,应该调用您的 ScopedAlertingService工作执行一次的地方。您的后台服务可以简单地永远循环,使用 await Task.Delay() .

    public class ScopedAlertingService : IScopedAlertingService
{
public async Task DoWork(CancellationToken stoppingToken)
{
// move contents of your AlertingController.GetAlertingToDB here
}
}

public class ConsumeScopedServiceHostedService : BackgroundService
{
private readonly IServiceProvider _services;
public ConsumeScopedServiceHostedService(IServiceProvider services)
{
_services = services;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(10000, stoppingToken);

using (var scope = _services.CreateScope())
{
var scopedProcessingService =
scope.ServiceProvider
.GetRequiredService<IScopedAlertingService>();

await scopedProcessingService.DoWork(stoppingToken);
}
}
}
}

关于c# - 如何创建在每个给定时间段运行一个函数的 BackGround 服务?使用 C#(asp.net 核心 3.1.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60822066/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com