我需要我的 Windows 服务在操作系统进入待机状态或低电量情况时做出优雅的反应。守旧派,我会绑定到 OnPowerEvents 事件,但我看不到如何在 .NET Framework 7 定义中执行此操作。任何人都有如何从托管应用程序服务的 Windows 服务订阅系统事件的工作示例?
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
我没有在 Host 对象、IServiceCollection 或 AddHostedService 定义中找到它。