实现服务操作的类及特定服务接口的连接配置问询
1. Does the class implementing service operations need to configure connections for each operation?
Not necessarily. It depends on how you structure your service class. If you follow Topshelf's interface-based convention (using the interfaces mentioned in your second question), you won't need to manually set up connections for each operation. Topshelf handles the wiring automatically behind the scenes.
That said, if you prefer to use custom method names instead of adhering to the interface method signatures, you would use HostConfigurator's WhenXXX methods (like WhenStarted, WhenStopped) to explicitly map your methods to the corresponding service lifecycle events.
2. If the class implements ServiceControl, ServiceSuspend, ServiceShutdown, ServiceCustomCommand, ServicePowerEvent, and/or ServiceSessionChange interfaces, do these operations still require configuration via HostConfigurator's WhenXXX methods?
Absolutely not. Topshelf is built to automatically detect these interfaces and bind their methods to the appropriate service events without any extra configuration. Here's a quick breakdown of how it works:
ServiceControl: Automatically linksStart()andStop()to the service's start/stop events.ServiceSuspend: MapsSuspend()andResume()to the suspend/resume events.ServiceShutdown: TriggersShutdown()when the system is shutting down.ServiceCustomCommand: Handles custom commands viaExecuteCustomCommand(int command).ServicePowerEvent: Responds to power events withPowerEvent(PowerBroadcastStatus status).ServiceSessionChange: Reacts to session changes usingSessionChange(SessionChangeDescription changeDescription).
If you do try to use WhenXXX methods alongside these interfaces, the explicit configuration will take precedence, but in most cases, relying on the interfaces is cleaner and reduces boilerplate code.
内容的提问来源于stack exchange,提问作者Paulo Morgado

