Writing a Windows Service in Rust
I’m currently writing a cross-platform server application in Rust which should also be able to run as a service on Windows. While this sounds like a relatively straight-forward thing to do, it was a bit more involved than I originally thought (especially compared to running unix services). Below you’ll find my notes and a well-documented code sample for running a small echo server for demonstration purposes as a Windows service. I’m going to cover service initialization, responding to control commands (like STOP) and also handling shutdowns in a tokio runtime setting where the individual tasks need to be cancelled. The sample application can also be run in console mode and the Windows service parts are conditionally compiled – so you can run/compile it on Unix systems for console mode use as well. ...