在其他完成工作后如何运行背景托管服务?
我正在创建应该是长期运行的后台服务的应用程序。它应该定期从一个地方获取数据并发送 API 请求。但这里有 2 项服务。第二个应该在第一个之后发送 API 请求。
是否可以在注册服务时使用某些配置来做到这一点?
或者是否只能作为单个服务调用第一个实例,然后调用第二个实例?
关于第一个服务 - 它从 A 地点获取数据,然后将 API 请求发送到 B 地点(以保存或更新现有数据)。
关于第二个服务 - 它从 A 地获取数据,然后将 API 请求发送到 B 地(以保存或更新现有数据)。但这里的数据与第一个请求的数据有关。因此,首先需要从第一个服务获取添加/更新数据。执行第二个服务后
示例: 第一个服务负责类别。 第二服务负责该类别的物品。
如果某些项目来自类别 XYZ,但数据库中不存在,则会导致错误。但是,如果第一个服务可以首先运行,则应将该类别添加到 Db 中。
我知道这也很棘手,因为我不知道在执行第二个服务之前是否添加/更新了第一个服务的数据。但更像是被保存了。
为了触发服务,我在 Program.cs 中有基本的后台服务
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
Console.WriteLine($"Started next iteration of {typeof(TSync).Name}");
using (var scope = _services.CreateAsyncScope())
{
var sync = scope.ServiceProvider.GetRequiredService<TSync>();
await sync.Run(DateTime.Now.AddDays(-_config.SyncStartShiftDays), false, _paramsFactory(sync));
Console.WriteLine($"Finished {typeof(TSync).Name}");
await Task.Delay(_config.SyncPeriodicity, stoppingToken);
}
}
}
:
var host = new HostBuilder()
.ConfigureHostConfiguration(configHost => { configHost.AddJsonFile("appsettings.json"); })
.ConfigureServices((hostContext, services) =>
{
// all registered services
})
.UseWindowsService()
.UseConsoleLifetime()
.Build();
var hostLifetime = host.Services.GetService<IHostApplicationLifetime>();
Task keyboardWatcher = null;
keyboardWatcher = Task.Factory.StartNew(() =>
{
while (true)
{
var key = Console.ReadKey();
if (key.KeyChar.ToString().ToUpper() == "C"
&& (key.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
{
hostLifetime.StopApplication();
break;
}
}
});
Console.WriteLine("Press Ctrl+C to exit");
host.Run();
I'm creating app that should be long-running background service. It should take data periodically from one place and send API request. But here are 2 services. 2nd should send API requests after the 1st one.
Is it possible to do this using some configurations while registering services?
Or is it only possible as a single service that should call 1st instance and then 2nd one?
About 1st service - It takes data from place A and then send API request to place B (to save or update existing data).
About 2nd service - It takes data from place A and then send API request to place B (to save or update existing data). But data here has relation to data from 1st request. So firstly need to get add/update data from 1st service. After execute 2nd service
Example:
1st service is responsible for Categories.
2nd service is responsible for Items from that categories.
In case some Item is from Category XYZ that isn't present in db, it will cause an error. But if 1st service can run firsly that category should be added to Db.
I understand that it's also tricky cause I don't know if data from 1st service is added/updated before execute 2nd service. But more like it's saved.
To trigger services I have basic background service
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
Console.WriteLine(quot;Started next iteration of {typeof(TSync).Name}");
using (var scope = _services.CreateAsyncScope())
{
var sync = scope.ServiceProvider.GetRequiredService<TSync>();
await sync.Run(DateTime.Now.AddDays(-_config.SyncStartShiftDays), false, _paramsFactory(sync));
Console.WriteLine(quot;Finished {typeof(TSync).Name}");
await Task.Delay(_config.SyncPeriodicity, stoppingToken);
}
}
}
In Program.cs:
var host = new HostBuilder()
.ConfigureHostConfiguration(configHost => { configHost.AddJsonFile("appsettings.json"); })
.ConfigureServices((hostContext, services) =>
{
// all registered services
})
.UseWindowsService()
.UseConsoleLifetime()
.Build();
var hostLifetime = host.Services.GetService<IHostApplicationLifetime>();
Task keyboardWatcher = null;
keyboardWatcher = Task.Factory.StartNew(() =>
{
while (true)
{
var key = Console.ReadKey();
if (key.KeyChar.ToString().ToUpper() == "C"
&& (key.Modifiers & ConsoleModifiers.Control) == ConsoleModifiers.Control)
{
hostLifetime.StopApplication();
break;
}
}
});
Console.WriteLine("Press Ctrl+C to exit");
host.Run();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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