5

i have regular C# service based on class ServiceBase. This service loads on startup c++ dynamic link library. Some times it happens that service crash in unmanaged code. Unfortunately Event viewer gives only very brief description of this. This is how looks one if his messages:

Application: StreamMapService.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: exception code c0000005, exception address 00000012".

With 99% certainty the problem is in unmanaged code. Problem is that this happen very rarely (typically once a day) and only when runned as service. Under debugger all is OK. To find out problematic code I edited my main method in following manner:

    static void Main()
    {
        try
        {
            if (!Environment.UserInteractive)
            {
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[] 
                { 
                    new Service1() 
                };
                ServiceBase.Run(ServicesToRun);
            }
            else
            {
                var services = new Service1();
                services.Start();
                Console.WriteLine("Press return to exit");
                Console.ReadLine();
                services.Stop();
            }
        }
        catch (SEHException e)
        {
            // wrapper for all exceptions having its origin in unmanaged code
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Crash time: {0}\n", DateTime.Now.ToString());
            sb.AppendFormat("\nMessage:\n{0}", e.Message);
            sb.AppendFormat("\nSource: {0}\n", e.Source);
            sb.AppendFormat("Stack trace:\n{0}", e.StackTrace);
            sb.AppendFormat("\nTarget site:{0}", e.TargetSite);

            SmtpClient client = new SmtpClient("mail.domain.com");
            MailAddress from = new MailAddress("sender@domain.com", "sender", System.Text.Encoding.UTF8);
            MailAddress to = new MailAddress("receiver@domain.com");
            MailMessage message = new MailMessage(from, to);
            message.Body = sb.ToString();
            message.Subject = "StreamMapService crash info";
            client.Send(message);
            throw;
        }
    }

To test this exception handler I generated test exception at some part of unmanaged code. All is functioning OK when I run service under debugger or from command line.

I receive email message with information about exception (most important it contains callstack where unmanaged exception originated).

But when I run this program as system service (from Control Panel -> Administrative Tools -> Services) the service clearly crashes (it becomes unresponsive on tcp interface and also event viewer contains crash information) but I receive none email.

It looks like exception handler isnt even called in this case. I tried to write exception info to file but this same happens.

So when runned under debugger or from command line, callstack information is correctly logged. When runned as system service nothing happens. It seems like exception handling is not functioning correctly in this condition.

truthseeker
  • 1,220
  • 4
  • 25
  • 58
  • open Command prompt and your debugger and run as admin, then tell the situation. Is it chrashing same as, when you run it as services. And before debugging change its framework version to below or equal 3.5 – PawanS Jan 25 '11 at 09:27

1 Answers1

3

The exception is probably thrown on another thread. Try adding the following before your code in order to catch any unhandled exceptions:

AppDomain.CurrentDomain.UnhandledException += 
  new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

The event handler should have the following signature:

void CurrentDomain_UnhandledException(
    object sender, UnhandledExceptionEventArgs e) {
}
Andreas Vendel
  • 716
  • 6
  • 14
  • This solved problem but only partially. When I examined my callstack more thoroughly i found that it doesnt contains unmanaged methods (information I have written in question is not correct sorry for this). – truthseeker Jan 25 '11 at 11:02
  • After some time it unexpectedly ceased to function. When running as regular exe file or under debugger exception handling is functioning correctly. – truthseeker Jan 25 '11 at 13:33
  • So the event AppDomain.UnhandledException is not raised? See http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx for more information about what could cause this. – Andreas Vendel Jan 25 '11 at 14:13
  • Because I am using .NET framework 4 I tried to add both HandleProcessCorruptedStateExceptions & SecurityCritical attributes to "main" and "CurrentDomain_UnhandledException" methods to no avail. – truthseeker Jan 26 '11 at 08:08
  • All is OK i have found that some of exceptions were swalloved. – truthseeker Jan 28 '11 at 11:56