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 view开发者_如何学Pythoner 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.
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) {
}
精彩评论