Минимальный рабочий пример (для использования с новым работником $ dotnet шаблон):
Код: Выделить всё
MyBackgroundService.csКод: Выделить всё
public class MyBackgroundService : BackgroundService {
private readonly IHostApplicationLifetime _lifetime;
public MyBackgroundService(IHostApplicationLifetime lifetime) => _lifetime = lifetime;
protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(async () => {
try {
while (!stoppingToken.IsCancellationRequested) {
await Task.Delay(1000);
if (Random.Shared.NextDouble() < 0.5) throw new Exception("RANDOM CRASH!");
}
}
catch {
throw; // I can hit a debug breakpoint here
}
finally {
_lifetime.StopApplication();
}
});
}
< /code>
Program.csКод: Выделить всё
public class Program {
public static int Main(string[] args) {
try {
var builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService();
var host = builder.Build();
host.Run();
return 0;
}
catch (Exception) {
return 1; //
When the app crashes, I expect the exit code to be 1Что может быть причиной?
Подробнее здесь: https://stackoverflow.com/questions/772 ... undservice
Мобильная версия