gpt4 book ai didi

c# - 如何为 Worker 服务安排任务

转载 作者:行者123 更新时间:2023-12-05 04:50:00 33 4
gpt4 key购买 nike

我正在实现 asp.net core 3.1 项目。我正在使用一个 Worker 服务,它应该每天从 SQL Server 表中读取一些数据,并调用一个应该发送短信的 api,而短信内容应该是那些从 sql server 读取的数据。现在我可以调用短信 api 并通过它向用户发送一些静态数据。问题是,虽然我定义了 api 应该每 5 秒调用一次,但它只发送 1 次短信给用户。如果有人告诉我如何解决这个问题,我将不胜感激。下面是我尝试过的:

public class Worker : BackgroundService, IHostedService, ISendingSMS
{
private readonly ILogger<Worker> _logger;


private MarketsContext _context;


public IServiceScopeFactory _serviceScopeFactory;
//---------------------------------------------------
public Report report { get; set; }
private MsgContent msgContent;
//---------------------------------------------------
public Worker(IServiceScopeFactory serviceScopeFactory)
{

_serviceScopeFactory = serviceScopeFactory;

}

//---------------------------------------------------------------------------------------
public async Task GetReport()
{

IQueryable<Report> reportData = _context.Report.Select(x => new Report
{
ReportDate = x.ReportDate,
TotalMarkets = x.TotalMarkets,

})/*.FirstOrDefault()*/;

report = await reportData.AsNoTracking().FirstOrDefaultAsync();
}
//-------------------------------------------------------------------------------
public override async Task StartAsync(CancellationToken cancellationToken)
{
// DO YOUR STUFF HERE
await base.StartAsync(cancellationToken);
}

public override async Task StopAsync(CancellationToken cancellationToken)
{
// DO YOUR STUFF HERE
await base.StopAsync(cancellationToken);
}

protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{

using (var scope = _serviceScopeFactory.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<MarketsContext>();

var _smssend = scope.ServiceProvider.GetRequiredService<SendingSMSService>();

_context = dbContext;

await GetReport();


var message = new MsgContent
{
Username = "cse.etsg",
Password = "wrwe",
From = "4500000",
To = "+429124203243",
Message = report.ReportDate + "" + + report.TotalMarkets
};


_smssend.sendSMS(message);

await Task.Delay(120000, stoppingToken);

//Do your stuff
}

}
}
}

发送短信服务

public class SendingSMSService /*: ISendingSMS*/
{

public Report report { get; set; }


// Here this method calls sms api sccessfully

public async void sendSMS(MsgContent message)
{



using (var client = new HttpClient())
{

var myContent = JsonConvert.SerializeObject(message);
var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

using (var result = client.PostAsync("https://api/SendSMS", byteContent).Result)
{
string Response = await result.Content.ReadAsStringAsync();

}
}

}
}

报告

public partial class Report
{
public int Id { get; set; }
public string ReportDate { get; set; }
public int? TotalMarkets { get; set; }

}

程序

public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{

var configuration = hostContext.Configuration;

services.AddHostedService<Worker>();
//------------------------------------------------------------------------------
services.AddScoped<SendingSMSService>();

var connection = hostContext.Configuration.GetConnectionString("Markets");
var optionsBuilder = new DbContextOptionsBuilder<MarketsContext>();
optionsBuilder.UseSqlServer(connection);
services.AddScoped<MarketsContext>(s => new MarketsContext(optionsBuilder.Options));


});
}

最佳答案

在这种情况下,您可以使用 Timed background tasktimed-background-tasks .

在您的情况下,您需要其中两个。它们是根据您 Timer 的参数安排的在StartAsync方法,您不必显式调用它们。

然后将创建的服务注入(inject)到 IHostBuilder.ConfigureServices (Program.cs) 中:services.AddHostedService<TimedHostedService>();

关于c# - 如何为 Worker 服务安排任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67433127/

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