shr*_*sha 3 c# wpf task .net-core ihostedservice
在 WPF 应用程序中,我配置了一个托管服务来在后台执行特定活动(参见本文)。这是在 App.xaml.cs 中配置托管服务的方式。
public App()
{
var environmentName = Environment.GetEnvironmentVariable("HEALTHBOOSTER_ENVIRONMENT") ?? "Development";
IConfigurationRoot configuration = SetupConfiguration(environmentName);
ConfigureLogger(configuration);
_host = Host.CreateDefaultBuilder()
.UseSerilog()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>()
.AddOptions()
.AddSingleton<IMailSender, MailSender>()
.AddSingleton<ITimeTracker, TimeTracker>()
.AddSingleton<NotificationViewModel, NotificationViewModel>()
.AddTransient<NotificationWindow, NotificationWindow>()
.Configure<AppSettings>(configuration.GetSection("AppSettings"));
}).Build();
AssemblyLoadContext.Default.Unloading += Default_Unloading;
Console.CancelKeyPress += Console_CancelKeyPress;
SystemEvents.PowerModeChanged += SystemEvents_PowerModeChanged;
}
Run Code Online (Sandbox Code Playgroud)
并开始启动
/// <summary>
/// Handles statup event
/// </summary>
/// <param name="e"></param>
protected override async void OnStartup(StartupEventArgs e)
{
try
{
Log.Debug("Starting the application");
await _host.StartAsync(_cancellationTokenSource.Token);
base.OnStartup(e);
}
catch (Exception ex)
{
Log.Error(ex, "Failed to start application");
await StopAsync();
}
}
Run Code Online (Sandbox Code Playgroud)
现在我想在系统进入睡眠状态时停止托管服务,并在系统恢复时重新启动该服务。我试过这个
/// <summary>
/// Handles system suspend and resume events
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
{
case PowerModes.Resume:
Log.Warning("System is resuming. Restarting the host");
try
{
_cancellationTokenSource = new CancellationTokenSource();
await _host.StartAsync(_cancellationTokenSource.Token);
}
catch (Exception ex)
{
Log.Error(ex, $"{ex.Message}");
}
break;
case PowerModes.Suspend:
Log.Warning("System is suspending. Canceling the activity");
_cancellationTokenSource.Cancel();
await _host.StopAsync(_cancellationTokenSource.Token);
break;
}
}
Run Code Online (Sandbox Code Playgroud)
停止主机工作正常,但是当主机重新启动时,我收到“ System.OperationCanceledException”。据我了解,托管服务生命周期与应用程序生命周期无关。我的理解有错吗?
这个问题- ASP.NET Core IHostedService 手动启动/停止/暂停(?)类似,但答案是根据配置暂停和重新启动服务,这看起来像是一个黑客,所以我正在寻找一种标准方法。
有什么想法吗?
正如Stephen Cleary在评论中指出的那样,工作人员可以独立于主机启动和停止,尽管理想情况下主机应该处理工作人员的生命周期。
但由于 .NET Core 3 中存在的错误,传递给IHostedService StartAsync方法的取消令牌不会传播到工作ExecuteAsync程序方法。我已经为此创建了一个问题,详细信息可以在这里找到 - https://github.com/dotnet/extensions/issues/3218
该错误的修复 ( https://github.com/dotnet/extensions/pull/2823 ) 将成为 .NET 5 的一部分,正如问题中所建议的那样 ( https://github.com/dotnet/extensions/issues/ ) 3218#issuecomment-622503957)我必须创建自己的类来模仿框架的BackGroundService类,该类将直接继承IHostedService取消标记并将其传播给工作人员。
BackGroundService类自定义实现在这里 -
/// <summary>
/// Base class for implementing a long running <see cref="IHostedService"/>.
/// </summary>
public abstract class BGService : IHostedService, IDisposable
{
private Task _executingTask;
private CancellationTokenSource _stoppingCts;
/// <summary>
/// This method is called when the <see cref="IHostedService"/> starts. The implementation should return a task that represents
/// the lifetime of the long running operation(s) being performed.
/// </summary>
/// <param name="stoppingToken">Triggered when <see cref="IHostedService.StopAsync(CancellationToken)"/> is called.</param>
/// <returns>A <see cref="Task"/> that represents the long running operations.</returns>
protected abstract Task ExecuteAsync(CancellationToken stoppingToken);
/// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
/// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
public virtual Task StartAsync(CancellationToken cancellationToken)
{
// Create linked token to allow cancelling executing task from provided token
_stoppingCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
// Store the task we're executing
_executingTask = ExecuteAsync(_stoppingCts.Token);
// If the task is completed then return it, this will bubble cancellation and failure to the caller
if (_executingTask.IsCompleted)
{
return _executingTask;
}
// Otherwise it's running
return Task.CompletedTask;
}
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// </summary>
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
public virtual async Task StopAsync(CancellationToken cancellationToken)
{
// Stop called without start
if (_executingTask == null)
{
return;
}
try
{
// Signal cancellation to the executing method
_stoppingCts.Cancel();
}
finally
{
// Wait until the task completes or the stop token triggers
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
}
public virtual void Dispose()
{
_stoppingCts?.Cancel();
}
}
Run Code Online (Sandbox Code Playgroud)
以及系统挂起和恢复时分别停止和启动工作程序的逻辑
/// <summary>
/// Handles system suspend and resume events
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
var workers = _host.Services.GetServices<IHostedService>();
Log.Information($"Found IHostedService instances - {workers.ToCSV()}");
switch (e.Mode)
{
case PowerModes.Resume:
Log.Warning("System is resuming. Restarting the workers");
try
{
_cancellationTokenSource = new CancellationTokenSource();
foreach (var worker in workers)
{
await worker.StartAsync(_cancellationTokenSource.Token);
}
}
catch (Exception ex)
{
Log.Error(ex, $"{ex.Message}");
}
break;
case PowerModes.Suspend:
Log.Warning("System is suspending. Stopping the workers");
_cancellationTokenSource.Cancel();
try
{
foreach (var worker in workers)
{
await worker.StopAsync(_cancellationTokenSource.Token);
}
}
catch (Exception ex)
{
Log.Error(ex, $"{ex.Message}");
}
break;
}
}
Run Code Online (Sandbox Code Playgroud)
请注意@davidfowl建议这不是受支持的功能(https://github.com/dotnet/extensions/issues/3218#issuecomment-623280990),但我没有遇到这种方法的任何问题,并且相信这也应该是受支持的用例。
| 归档时间: |
|
| 查看次数: |
12544 次 |
| 最近记录: |