Fixed an unhandled exception when running as a service.