Skip to content

Commit 75a4852

Browse files
committed
Update report converter to stop cleanly
1 parent fef4184 commit 75a4852

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

CSF.Screenplay.JsonToHtmlReport/ReportConverterApplication.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,45 @@ namespace CSF.Screenplay.JsonToHtmlReport
1111
/// <summary>
1212
/// An application/background service that begins the JSON to HTML report conversion process.
1313
/// </summary>
14-
public class ReportConverterApplication : BackgroundService
14+
public class ReportConverterApplication : IHostedService
1515
{
1616
readonly IOptions<ReportConverterOptions> options;
1717
readonly IConvertsReportJsonToHtml reportConverter;
18+
readonly IHostApplicationLifetime lifetime;
1819

1920
/// <summary>
2021
/// Initializes a new instance of the <see cref="ReportConverterApplication"/> class.
2122
/// </summary>
2223
/// <param name="options">The options for performing the conversion.</param>
2324
/// <param name="reportConverter">The report converter instance to use for conversion.</param>
25+
/// <param name="lifetime">The application lifetime</param>
2426
public ReportConverterApplication(IOptions<ReportConverterOptions> options,
25-
IConvertsReportJsonToHtml reportConverter)
27+
IConvertsReportJsonToHtml reportConverter,
28+
IHostApplicationLifetime lifetime)
2629
{
27-
this.options = options ?? throw new System.ArgumentNullException(nameof(options));
28-
this.reportConverter = reportConverter ?? throw new System.ArgumentNullException(nameof(reportConverter));
30+
this.options = options ?? throw new ArgumentNullException(nameof(options));
31+
this.reportConverter = reportConverter ?? throw new ArgumentNullException(nameof(reportConverter));
32+
this.lifetime = lifetime ?? throw new ArgumentNullException(nameof(lifetime));
2933
}
3034

3135
/// <summary>
32-
/// Executes the background service operation.
36+
/// Executes the application, to perform its work.
3337
/// </summary>
34-
/// <param name="stoppingToken">A token that can be used to stop the operation.</param>
35-
/// <returns>A task that represents the asynchronous operation.</returns>
36-
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
38+
/// <param name="cancellationToken">A cancellation token</param>
39+
/// <returns>A task</returns>
40+
public async Task StartAsync(CancellationToken cancellationToken)
3741
{
3842
await reportConverter.ConvertAsync(options.Value);
3943
Console.WriteLine("Conversion complete; HTML report available at {0}", options.Value.OutputPath);
40-
Environment.Exit(0);
44+
lifetime.StopApplication();
4145
}
46+
47+
/// <summary>
48+
/// Unused, always returns a completed task.
49+
/// </summary>
50+
/// <param name="cancellationToken">A cancellation token</param>
51+
/// <returns>A task</returns>
52+
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
4253
}
4354
}
4455

0 commit comments

Comments
 (0)