c# - Windows service exception not handled -
i have regular c# service based on class servicebase. service loads on startup c++ dynamic link library. times happens service crash in unmanaged code. unfortunately event viewer gives brief description of this. how looks 1 if messages:
application: streammapservice.exe framework version: v4.0.30319 description: process terminated due unhandled exception. exception info: exception code c0000005, exception address 00000012".
with 99% certainty problem in unmanaged code. problem happen (typically once day) , when runned service. under debugger ok. find out problematic code edited 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 exit"); console.readline(); services.stop(); } } catch (sehexception e) { // wrapper exceptions having 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 = new mailaddress("sender@domain.com", "sender", system.text.encoding.utf8); mailaddress = 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 exception handler generated test exception @ part of unmanaged code. functioning ok when run service under debugger or command line.
i receive email message information exception (most important contains callstack unmanaged exception originated).
but when run program system service (from control panel -> administrative tools -> services) service crashes (it becomes unresponsive on tcp interface , event viewer contains crash information) receive none email.
it looks exception handler isnt called in case. tried write exception info file same happens.
so when runned under debugger or command line, callstack information correctly logged. when runned system service nothing happens. seems exception handling not functioning correctly in condition.
the exception thrown on thread. try adding following before code in order catch unhandled exceptions:
appdomain.currentdomain.unhandledexception += new unhandledexceptioneventhandler(currentdomain_unhandledexception);
the event handler should have following signature:
void currentdomain_unhandledexception( object sender, unhandledexceptioneventargs e) { }
Comments
Post a Comment