jeudi 16 avril 2015

System.Reflection.TargetInvocationException not being caught

NOTE: This is OUTSIDE of the IDE. The referenced question is NOT the same.


I am having a problem with an exception that occurs in a method that gets called by reflection. The exception itself actually occurs in the called method, but since the method is called by reflection, the real exception gets wrapped in a System.Reflection.TargetInvocationException. No problem, just catch it and get the internal exception - except the System.Reflection.TargetInvocationException doesn't get caught. My program crashes, and I get a dump along with an entry in the Windows event log.


The Windows event log doesn't contain the internal exceptions, and neither does the dump. I can't attach a debugger to the program because then an external library (which is needed to get to the reflection call) won't run - copy protection, don't you know. If I put a try/catch into the offending method, the exception doesn't occur - which is bad. The cause isn't fixed, it just doesn't happen any more. The same effect happens if I put logging into the offending method - the exception doesn't occur any more.


I can't use logging, I can't use a debugger, and in the one place where I could catch the exception and log it the exception doesn't get caught.


I'm using Visual Studio 2010 and dotnet 4.0.


To make it clear: The try/catch doesn't work when the program is run outside of Visual Studio, and I can't run it inside of Visual Studio in the debugger because then the program can't reach the point where the exception occurs. This is NOT within the IDE.


Eliminating the reflection isn't an option (I've tried it for just the one case, and the exception goes away.)


The method being called does a lot of stuff, but breaking it down into smaller methods doesn't help - the exception just goes away.


The exception doesn't occur all the time, only when I do a certain sequence of steps - and when it occurs then it is always on the second time through the whole sequence.


In the sequence I am using, the method gets called by two threads almost simultaneously - a particular bunch of data gets entered, which causes a copy of a report and another document to be printed on two separate printers - one report and document to each printer. Since generating the reports and printing the document can take a while, they are done on threads in the background so the user can continue working.


I suspect that the threads are stepping on each others toes (lots of file and database manipulations going on) but without knowing what is really happening I can't fix it.


The code below shows a simplified version of the call by reflection.


Does any one have a suggestion as to what may be causing the System.Reflection.TargetInvocationException to not be caught, or maybe an alternative way to catch the inner exception?



Try
Dim methode As System.Reflection.MethodInfo
methode = GetType(AParticularClass).GetMethod("OneOfManyMethods", Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static)
Dim resultobject As Object = methode.Invoke(Nothing, Reflection.BindingFlags.InvokeMethod Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static, Nothing, New Object() {SomeBooleanVariable, SomeStringVariable}, Nothing)
result = DirectCast(resultobject, DataSet)
Catch ex As Exception
'Log the error here.
End Try





Aucun commentaire:

Enregistrer un commentaire