|
6 | 6 |
|
7 | 7 | namespace CodeCaster.WindowsServiceExtensions |
8 | 8 | { |
| 9 | + /// <summary> |
| 10 | + /// Extension method for setting up <see cref="PowerEventAwareWindowsServiceLifetime"/>. |
| 11 | + /// </summary> |
9 | 12 | public static class WindowsServiceLifetimeHostBuilderExtensionsAdapter |
10 | 13 | { |
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) |
12 | 33 | { |
13 | 34 | if (!WindowsServiceHelpers.IsWindowsService()) |
14 | 35 | { |
15 | | - return builder; |
| 36 | + return hostBuilder; |
16 | 37 | } |
17 | 38 |
|
18 | 39 | // Call MS's one |
19 | | - builder.UseWindowsService(); |
| 40 | + hostBuilder.UseWindowsService(configure); |
20 | 41 |
|
21 | | - return builder.ConfigureServices(services => |
| 42 | + return hostBuilder.ConfigureServices(services => |
22 | 43 | { |
23 | 44 | // Replace UseWindowsService()'s IHostLifetime lifetime with our own |
24 | 45 | var lifetime = services.FirstOrDefault(s => s.ImplementationType == typeof(WindowsServiceLifetime)); |
|
0 commit comments