How can I configure .NET handling UnhandledException in a windows service?

protected override void OnStart(string[] args) { AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); Thread.Sleep(10000); throw new Exception(); } void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { } 

I connected the debugger to the above code in my Windows service, setting a breakpoint in CurrentDomain_UnhandledException, but it never got caught. An exception appears, saying that it is raw, and then the service stops. I even tried putting some code in an event handler if it was optimized.

Are you setting up unhandled exception handling in a Windows service?

+41
c # exception-handling windows-services unhandled-exception
Mar 16 '10 at 17:54
source share
5 answers

I came to this topic quite late, but I thought it might be useful to give an explanation that does not give any of the other answers.

The reason that the CurrentDomain_UnhandledException handler CurrentDomain_UnhandledException not fall into the OP code sample is because the OnStart method is called in response to the Start command from the Windows Service Control Manager (the command was received and sent to this method as part of the ServiceBase Framework implementation); any exception that OnStart is handled in the base class, logged in the event log, and converted to the error status code returned to SCM. Thus, the exception never extends to the AppDomain's unhandled exception handler.

I think you will find that the handled CurrentDomain_UnhandledException handled by an unhandled exception from the workflow in your service.

+50
Feb 25 2018-11-21T00:
source share

On a Windows service, you do not want to run a lot of code in the OnStart method. All you need is code to start the service flow and return.

If you do, you can handle exceptions that occur in your service flow, just fine.

eg.

 public static void Start() { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(currentDomain_UnhandledException); running = true; ThreadStart ts = new ThreadStart(ServiceThreadBody); thread = new Thread(ts); thread.Name = "ServiceThread"; thread.Priority = ThreadPriority.BelowNormal; thread.Start(); } 
+8
Mar 26
source share

Know that this thread is a bit outdated, but thought it would be useful to add some comments based on personal experience developing Windows services in .NET. The best approach is to avoid developing in the Service Management Manager as much as possible . To do this, you need a simple wiring that mimics the way services are started - something that can create an instance of its service class (which is already pulled from ServiceBase) and call the OnStart, OnStop methods, etc. This harness can be a console application or a Windows application of your choice.

This is pretty much the only way to find problems starting a debugging service in .NET — the interaction between your code, Visual Studio, and the actual service control manager simply makes the process impossible otherwise.

NTN.

+4
Aug 26 '10 at 10:34
source share

Just curious what you're trying to accomplish: avoiding service failures or error messages?

For reporting, I think your best bet is to add statements at the highest try / catch level. You can try to register them in the Windows event log and / or in the log file.

You can also set the ExitCode property to a non-zero value until you stop the service. If the system administrator starts your service from the Services control panel and your service suddenly stops with a non-zero exit code, Windows may display an error message describing the error.

+3
Mar 16 '10 at 18:38
source share

When I worked on my own Windows service, it stopped on its own rather oddly. I thought this was due to an unprepared exception. At the moment I am in obscure exceptions from a text file. First of all, you need to create a new ServiceLog.txt file in C locations due to the registration of fakes in a text file. With the following coding, I got all exceptions with line numbers.

 using System.Security.Permissions; using System.IO; [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)] protected override void OnStart(string[] args) { AppDomain currentDomain = AppDomain.CurrentDomain; currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler); ... Your codes... .... } void MyHandler(object sender, UnhandledExceptionEventArgs args) { Exception e = (Exception)args.ExceptionObject; WriteToFile("Simple Service Error on: {0} " + e.Message + e.StackTrace); } private void WriteToFile(string text) { string path = "C:\\ServiceLog.txt"; using (StreamWriter writer = new StreamWriter(path, true)) { writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))); writer.Close(); } } 
+2
Aug 12 '17 at 13:34 on
source share



All Articles