6

I want to subscribe to the AppDomain.CurrentDomain.UnhandledException event to catch any errors as im trying a new design pattern where I dont want to put a try catch on every event.

usually in a win forms app there is a main entry point, but this is a class library that is called by another application.

Is there a main entry point in a class library where I can hook up this event, or is it just not possible in a class library unless im sure one method will get called before anything else?

WraithNath
  • 17,658
  • 10
  • 55
  • 82
  • @Jodrell - thanks, but can I use the [STAThread] attribute to define a location in my assembly that will automaically get called. This isnt an executable, and the calling application can instantiate any form in any order. Which is why I would need to subscribe to this event before any of that can happen – WraithNath Jun 09 '11 at 14:11
  • Somthing like hooking for .Net apps – Jodrell Jun 09 '11 at 14:29
  • 1
    you could do something along these lines http://www.codingthewheel.com/archives/how-to-inject-a-managed-assembly-dll – Jodrell Jun 09 '11 at 14:35
  • @Jodrell, interesting read. A bit overkill for what im doing though :) thanks anyways – WraithNath Jun 09 '11 at 14:49

2 Answers2

1

No, class libraries don't have a main entry point. If you feel like you need one, then you could conceptually create one virtually via your public API surface area. In other words, limit what objects are public and in those public objects make sure the call gets made at some point. That could be taken to the extreme of requiring a factory call of some kind to setup your library before doing anything.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • Thanks, thats what I thought. I was hoping there would be some kind of event I could hook into when the assembly iteself is loaded. Unfortunately I cant hook into anything from the calling application. Looks like this will have to wait until my next executable – WraithNath Jun 09 '11 at 14:18
0
// Set the unhandled exception mode to force errors to go through our handler. 
Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

// Add the event handler for handling thread exceptions to the event.
AppDomain.CurrentDomain.UnhandledException += 
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Thanks to Brian Dishaw

Clearly this does not give you some non declarative way of assigning the handler, ala assingning a trace listener in app.config but, that gives me an idea.

You could write your own configuration section that would perform this code. Then when the config is loaded you could assign the event handler, I'll find some links on how to do that ...

However, your configuration would have to be present in the main app so perhaps this is not right.

EDIT Sorry, I don't think there is way to do this without a IoC framework of some kind, what else would load your class. The only options I can think of all require some sort of change to the main application.

I'm interested in being wrong.

Community
  • 1
  • 1
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • Thanks - I know about the unhandled exception event already (its in the title of the question). My question is whether there is a entry point in the class library where I can hook this up before anything else gets called. – WraithNath Jun 09 '11 at 14:13
  • I've got your point now, and I've added a bit extra, more coming – Jodrell Jun 09 '11 at 14:17
  • I fail, can't think of anything that meets the brief. – Jodrell Jun 09 '11 at 14:26
  • @Jodril - no problem, thanks for your input anyway. I didnt think it was possible, I just thought I would ask :) – WraithNath Jun 09 '11 at 14:29