Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The Scenario:

I have a service that instantiates an object using Activator.GetObject whose type cannot be known until runtime. All we know ahead of time is that it implements the appropriate interface. (Assembly and Type identification information comes from other received data.) The new object connects to some external device (serial port, TCP socket, whatever) and handles communication with it.

The Problem:

If an exception goes unhandled in one of the threads of that object, where and how can I catch it in my service?

I have tried a handler for AppDomain.CurrentDomain.UnhandledException in the service object, the object that performs the instantiation, and in the object itself, but none of these will catch the NullReferenceException that I injected for my testing. (I had it thrown upon retrieving one of the object's properties that the instantiating object accesses from a timer event.) What other options are there?

Further discoveries: The UnhandledException subscription works for exceptions that happen before the call to ServiceBase.Run() if the scbscription is placed in Main() or the ServiceBase object constructor. But those two (usually trivial) methods are the only things that happen in that space. Once the service is being Started, no subscription works anywhere regardless of where the exception is thrown--even in the very next statement after the subscription.
Posted
Updated 7-Sep-10 4:52am
v5

1 solution

Try Googling 'windows service unhandled exception'. Of the hits I got, the first answer in http://webcache.googleusercontent.com/search?q=cache:KnK7iUFzTHEJ:stackoverflow.com/questions/58280/unhandledexception-handler-in-a-net-windows-service+windows+service+unhandled+exception&cd=4&hl=en&ct=clnk&gl=us[^] appears to shed some light on why the AppDomain unhandled exception handler isn't responding.
 
Share this answer
 
v2
Comments
John Whitmire 19-Aug-10 16:07pm    
Following your lead, for which I am grateful, I have found several similar forum threads, most of which end up where I currently find myself: at a dead end. Someone asks the question about catching these things, a responder says that they use the UnhandledException event, and either the submittor or a third person responds that it won't work for them. And the trail ends there.

The link you provided does provide a few clues, but hasn't (yet) led me to any answers in two days of heavy searching. I can't get the UnhandledException event to fire for anything that happens after the OnStart method completes. In fact, I can't even get anything logged anywhere about the unhandled exception... except in the VS debugger.

The UnhandledException event is working for some people. I want to learn what obscure thing they are doing and I'm not that makes the difference.
John Whitmire 19-Aug-10 16:09pm    
Reason for my vote of 3
Helpful, but not yet an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900