In dotnet 3.1, I had the code below, and everything worked fine. However, in 5.0, I think the stopping order has changed, so only the OnStarted function is properly called,