Could someone help me figure out this error with my Windows service project?
I can run this service successfully, it works perfectly but any time the service is stopped it shows this error:
C:WINDOWSsystem32>sc start EmailAutoResponseService
[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion.
This error is shown AFTER OnStop() is called. I’m assuming it has something to do with my OnStart method and/or the way I’m using async/await.
Could someone please point me in the right direction? Feel free to make other suggestions too, is async/await unnecessary here?
This is my Program.cs file:
And this my EmailAutoResponseService.cs file:
These are the relevant methods:
/// <summary>
/// Initializes a new instance of the EmailAutoResponseService class.
/// </summary>
public EmailAutoResponseService()
{
InitializeComponent();
LoadDependencies();
_cts = new CancellationTokenSource();
}
protected override void OnStart(string[] args)
{
LogTs.LogInfo(“Running OnStart().”);
Task.Run(async () => await StartServiceAsync());
}
protected override void OnStop()
{
LogTs.LogInfo(“Running OnStop().”);
Task.Run(async () => await StopServiceAsync()).Wait();
}
public async Task StartServiceAsync()
{
LogTs.LogInfo(“Running StartServiceAsync().”);
await RunEmailProcessingLoop(_cts.Token);
}
/// <summary>
/// Stops the service asynchronously.
/// </summary>
public async Task StopServiceAsync()
{
LogTs.LogInfo(“Running StopServiceAsync().”);
_cts.Cancel();
// Give some time for the cancellation to be processed
await Task.Delay(1000);
}
I can run this service successfully, it works perfectly but any time the service is stopped it shows this error: C:WINDOWSsystem32>sc start EmailAutoResponseService[SC] StartService FAILED 1053:The service did not respond to the start or control request in a timely fashion. This error is shown AFTER OnStop() is called. I’m assuming it has something to do with my OnStart method and/or the way I’m using async/await. Could someone please point me in the right direction? Feel free to make other suggestions too, is async/await unnecessary here? This is my Program.cs file: https://pastebin.com/h4UhmuZD And this my EmailAutoResponseService.cs file: https://pastebin.com/ApAgUXph These are the relevant methods: /// <summary>/// Initializes a new instance of the EmailAutoResponseService class./// </summary>public EmailAutoResponseService(){InitializeComponent();LoadDependencies();_cts = new CancellationTokenSource();}protected override void OnStart(string[] args){LogTs.LogInfo(“Running OnStart().”);Task.Run(async () => await StartServiceAsync());}protected override void OnStop(){LogTs.LogInfo(“Running OnStop().”);Task.Run(async () => await StopServiceAsync()).Wait();}public async Task StartServiceAsync(){LogTs.LogInfo(“Running StartServiceAsync().”);await RunEmailProcessingLoop(_cts.Token);}/// <summary>/// Stops the service asynchronously./// </summary>public async Task StopServiceAsync(){LogTs.LogInfo(“Running StopServiceAsync().”);_cts.Cancel();// Give some time for the cancellation to be processedawait Task.Delay(1000);} Read More