Skip to content

Commit a22cfe0

Browse files
committed
Clean up code (the bool from bool OnPowerEvent is ignored anyway), add comments.
1 parent 0311f4a commit a22cfe0

4 files changed

Lines changed: 20 additions & 15 deletions

File tree

src/CodeCaster.WindowsServiceExtensions/IPowerEventAwareHostedService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33

44
namespace CodeCaster.WindowsServiceExtensions
55
{
6+
/// <summary>
7+
/// An <see cref="IHostedService"/> that runs as a Windows Service that can react to power state changes.
8+
/// </summary>
69
public interface IPowerEventAwareHostedService : IHostedService
710
{
8-
bool OnPowerEvent(PowerBroadcastStatus powerStatus);
11+
/// <summary>
12+
/// Called when a power event is sent by the OS.
13+
/// </summary>
14+
void OnPowerEvent(PowerBroadcastStatus powerStatus);
915
}
1016
}

src/CodeCaster.WindowsServiceExtensions/PowerEventAwareBackgroundService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33

44
namespace CodeCaster.WindowsServiceExtensions
55
{
6+
/// <summary>
7+
/// Base class for implementing a long running <see cref="IHostedService"/> as a Windows Service that can react to power state changes.
8+
/// </summary>
69
public abstract class PowerEventAwareBackgroundService : BackgroundService, IPowerEventAwareHostedService
710
{
811
/// <summary>
912
/// Override this method to react to power state changes.
1013
/// </summary>
11-
/// <returns>Return false to deny the power change status request.</returns>
12-
public virtual bool OnPowerEvent(PowerBroadcastStatus powerStatus)
14+
public virtual void OnPowerEvent(PowerBroadcastStatus powerStatus)
1315
{
14-
return true;
1516
}
1617
}
1718
}

src/CodeCaster.WindowsServiceExtensions/PowerEventAwareWindowsServiceLifetime.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace CodeCaster.WindowsServiceExtensions
1414
{
1515
/// <summary>
16-
/// Basically the same as <see cref="WindowsServiceLifetime"/>, but responds to
16+
/// Basically the same as <see cref="WindowsServiceLifetime"/>, but states it can handle power events, and forwards those to the IHostedServices that claim to be capable of the same.
1717
/// </summary>
1818
public class PowerEventAwareWindowsServiceLifetime : WindowsServiceLifetime, IHostLifetime
1919
{
@@ -67,6 +67,7 @@ public PowerEventAwareWindowsServiceLifetime(IServiceProvider serviceProvider, I
6767
}
6868
}
6969

70+
/// <inheritdoc />
7071
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
7172
{
7273
_logger.LogInformation($"Windows Service power event: {powerStatus}");
@@ -77,18 +78,14 @@ protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
7778
return true;
7879
}
7980

80-
var acceptEvent = true;
81-
81+
// Forward the event to all registered I(PowerEventAware)HostedServices.
8282
foreach (var service in _hostedServices)
8383
{
84-
if (!service.OnPowerEvent(powerStatus))
85-
{
86-
// If any of the hosted services returns false, we deny the power status change request by returning false from this method (does that even work?)
87-
acceptEvent = false;
88-
}
84+
service.OnPowerEvent(powerStatus);
8985
}
9086

91-
return acceptEvent;
87+
// Ignored anyway.
88+
return true;
9289
}
9390

9491
protected override void OnStart(string[] args)
@@ -104,6 +101,7 @@ protected override void OnStart(string[] args)
104101
if (!_applicationLifetime.ApplicationStarted.IsCancellationRequested)
105102
{
106103
// Usually very early in the startup process, so this may not be logged nor reported at all.
104+
// But it's here to prevent the service from happily reporting successful startup, while the .NET Core ApplicationHost isn't started at all.
107105
const string errorString = "Windows Service failed to start";
108106
_logger.LogError(errorString);
109107
throw new Exception(errorString);

src/CodeCaster.WindowsServiceExtensions/WindowsServiceLifetimeHostBuilderExtensionsAdapter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace CodeCaster.WindowsServiceExtensions
1212
public static class WindowsServiceLifetimeHostBuilderExtensionsAdapter
1313
{
1414
/// <summary>
15-
/// Sets the host lifetime to <see cref="PowerEventAwareWindowsServiceLifetime"/> and does whatever <see cref="WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(Microsoft.Extensions.Hosting.IHostBuilder)"/> does.
15+
/// Sets the host lifetime to <see cref="PowerEventAwareWindowsServiceLifetime"/> and does whatever <see cref="WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder)"/> does.
1616
/// </summary>
1717
/// <param name="hostBuilder">The Microsoft.Extensions.Hosting.IHostBuilder to operate on.</param>
1818
/// <returns>The same instance of the Microsoft.Extensions.Hosting.IHostBuilder for chaining.</returns>
@@ -23,7 +23,7 @@ public static IHostBuilder UsePowerEventAwareWindowsService(this IHostBuilder ho
2323
}
2424

2525
/// <summary>
26-
/// Sets the host lifetime to <see cref="PowerEventAwareWindowsServiceLifetime"/> and does whatever <see cref="WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(Microsoft.Extensions.Hosting.IHostBuilder)"/> does.
26+
/// Sets the host lifetime to <see cref="PowerEventAwareWindowsServiceLifetime"/> and does whatever <see cref="WindowsServiceLifetimeHostBuilderExtensions.UseWindowsService(IHostBuilder)"/> does.
2727
/// </summary>
2828
/// <param name="hostBuilder">The Microsoft.Extensions.Hosting.IHostBuilder to operate on.</param>
2929
/// <param name="configure">An action to configure the lifetime's options.</param>

0 commit comments

Comments
 (0)