vb.net - UnhandledException handler in a .Net Windows Service -
is possible use unhandledexception handler in windows service?
normally use custom built exception handling component logging, phone home, etc. component adds handler system.appdomain.currentdomain.unhandledexception far can tell doesn’t achieve win windows service end pattern in 2 (or 4) service entry points:
protected overrides sub onstart(byval args() string) ' add code here start service. method should set things ' in motion service can work. try myservicecomponent.start() catch ex exception 'call our exception handler myexceptionhandlingcomponent.manuallyhandleexception (ex) 'zero default exitcode successfull exit, if set non-zero exitcode = -1 'so, use environment.exit, seems appropriate thing use 'we pass exit code here well, in case. system.environment.exit(-1) end try end sub
is there way custom exception handling component can deal better don't have fill onstart messy exception handling plumbing?
ok, i’ve done little more research now. when create windows service in .net, create class inherits system.serviceprocess.servicebase (in vb hidden in .designer.vb file). override onstart , onstop function, , onpause , oncontinue if choose to. these methods invoked within base class did little poking around reflector. onstart invoked method in system.serviceprocess.servicebase called servicequeuedmaincallback. vesion on machine "system.serviceprocess, version=2.0.0.0" decompiles this:
private sub servicequeuedmaincallback(byval state object) dim args string() = directcast(state, string()) try me.onstart(args) me.writeeventlogentry(res.getstring("startsuccessful")) me.status.checkpoint = 0 me.status.waithint = 0 me.status.currentstate = 4 catch exception exception me.writeeventlogentry(res.getstring("startfailed", new object() { exception.tostring }), eventlogentrytype.error) me.status.currentstate = 1 catch obj1 object me.writeeventlogentry(res.getstring("startfailed", new object() { string.empty }), eventlogentrytype.error) me.status.currentstate = 1 end try me.startcompletedsignal.set end sub
so because me.onstart(args) called within try portion of try catch block assume happens within onstart method wrapped try catch block , therefore exceptions occur aren't technically unhandled handled in servicequeuedmaincallback try catch. currentdomain.unhandledexception never happens @ least during startup routine. other 3 entry points (onstop, onpause , oncontinue) called base class in similar way.
so ‘think’ explains why exception handling component can’t catch unhandledexception on start , stop, i’m not sure if explains why timers setup in onstart can’t cause unhandledexception when fire.
Comments
Post a Comment