I have a Windows Service built in C# using .NET which every time a user logs off, suspends, sleeps resets or powers down it throws an error. In the Application Event Viewer it shows the error as this :-
Application: Service.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ComponentModel.Win32Exception
Exception Info: System.InvalidOperationException
at System.ServiceProcess.ServiceController.GenerateNames()
at System.ServiceProcess.ServiceController.get_ServiceName()
at System.ServiceProcess.ServiceController.Stop()
at WellformationDesktopService.Wellformation+<OnSessionChange>d__3.MoveNext()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__6_1(System.Object)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
The action that seems to be failing is using a System.Net.HTTP.HttpClient to send a submission to remote API. The code works at any other time other than when Windows is in effect shutting down the user space.
What do I need to do to force my service to fire the action before it is not possible to do?
Thanks in advance.
We have corrected the issue as per the advice from @Martheen above and all is now working. We ignore the windows shut down events but on startup find the last logout for a user and use that time for the end of their session and submit it in retrospect the next time the machine is used.
Thanks