Skip to content

Commit 0311f4a

Browse files
committed
WIP comments
1 parent 1b14723 commit 0311f4a

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/CodeCaster.WindowsServiceExtensions/PowerEventAwareWindowsServiceLifetime.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
namespace CodeCaster.WindowsServiceExtensions
1414
{
15+
/// <summary>
16+
/// Basically the same as <see cref="WindowsServiceLifetime"/>, but responds to
17+
/// </summary>
1518
public class PowerEventAwareWindowsServiceLifetime : WindowsServiceLifetime, IHostLifetime
1619
{
1720
private readonly CancellationTokenSource _starting = new();

src/CodeCaster.WindowsServiceExtensions/WindowsServiceLifetimeHostBuilderExtensionsAdapter.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,40 @@
66

77
namespace CodeCaster.WindowsServiceExtensions
88
{
9+
/// <summary>
10+
/// Extension method for setting up <see cref="PowerEventAwareWindowsServiceLifetime"/>.
11+
/// </summary>
912
public static class WindowsServiceLifetimeHostBuilderExtensionsAdapter
1013
{
11-
public static IHostBuilder UsePowerEventAwareWindowsService(this IHostBuilder builder)
14+
/// <summary>
15+
/// Sets the host lifetime to <see cref="PowerEventAwareWindowsServiceLifetime"/> and does whatever <see cref="WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(Microsoft.Extensions.Hosting.IHostBuilder)"/> does.
16+
/// </summary>
17+
/// <param name="hostBuilder">The Microsoft.Extensions.Hosting.IHostBuilder to operate on.</param>
18+
/// <returns>The same instance of the Microsoft.Extensions.Hosting.IHostBuilder for chaining.</returns>
19+
/// <remarks>This is context aware and will only activate if it detects the process is running as a Windows Service.</remarks>
20+
public static IHostBuilder UsePowerEventAwareWindowsService(this IHostBuilder hostBuilder)
21+
{
22+
return UsePowerEventAwareWindowsService(hostBuilder, _ => { });
23+
}
24+
25+
/// <summary>
26+
/// Sets the host lifetime to <see cref="PowerEventAwareWindowsServiceLifetime"/> and does whatever <see cref="WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(Microsoft.Extensions.Hosting.IHostBuilder)"/> does.
27+
/// </summary>
28+
/// <param name="hostBuilder">The Microsoft.Extensions.Hosting.IHostBuilder to operate on.</param>
29+
/// <param name="configure">An action to configure the lifetime's options.</param>
30+
/// <returns>The same instance of the Microsoft.Extensions.Hosting.IHostBuilder for chaining.</returns>
31+
/// <remarks>This is context aware and will only activate if it detects the process is running as a Windows Service.</remarks>
32+
public static IHostBuilder UsePowerEventAwareWindowsService(this IHostBuilder hostBuilder, Action<WindowsServiceLifetimeOptions> configure)
1233
{
1334
if (!WindowsServiceHelpers.IsWindowsService())
1435
{
15-
return builder;
36+
return hostBuilder;
1637
}
1738

1839
// Call MS's one
19-
builder.UseWindowsService();
40+
hostBuilder.UseWindowsService(configure);
2041

21-
return builder.ConfigureServices(services =>
42+
return hostBuilder.ConfigureServices(services =>
2243
{
2344
// Replace UseWindowsService()'s IHostLifetime lifetime with our own
2445
var lifetime = services.FirstOrDefault(s => s.ImplementationType == typeof(WindowsServiceLifetime));

0 commit comments

Comments
 (0)