I do sometimes run in a problem when distributing some C#/managed test tools to my workmates, were still an older version of another assembly is installed, but the newer one required version is missing.
Starting the application from within the IDE (as debug version) clearly shows a System.IO.FileNotFoundException for the mssing assembly in the debug console. Now whenI try to start the same application as a release version, nothing happens, meaning the appications silently (no message /alert on the desktop) starts up and immediatly closes., assumingly when the missing assembly has been caught.
Is there a way to intercept the "silent" exception handler in the runtime libraries, so that i can show the reason of the exception as it pops up for the debug version in the IDE's debug console?
We are talking .Net framework 2.0 here.
Regards
albert200 - Edited byalbert200 Monday, September 21, 2009 4:04 PM
-
|
| albert200 |
I suspect that your Main method is still not quite right since you should never see the CLR-generated error dialog if your code handles the exception completely. Could you please post a short but complete sample ( http://www.yoda.arachsys.com/csharp/complete.html) that demonstrates the behaviour you described in your last post? - Marked As Answer byalbert200 Tuesday, September 22, 2009 4:46 PM
-
|
| Nicole Calinoiu |
AFAIK, there is no "silent" handler that would be hiding this sort of problem entirely. Are you sure that you don't have any top-level exception handler in your application that might be swallowing the exception?
Regardless of the above issue, if you want to deal with assembly load failures, you can subscribe to the AppDomain.AssemblyResolve event. |
| Nicole Calinoiu |
AFAIK, i am not yet in control, if the exception rises (in debug mode). If i set a breakpoint at the very entry of Main from program.cs, allI can see is the exception debug console output andI never hit the breakpoint.
I did not declare any "private" exception handlers....as far as i know
Regards
albert200 |
| albert200 |
References to the assembly in need are given by right-clicking the References folder in Visual Studio 2008's solution explorer for the given C# project and browsing to the required assembly...it is not about loading assemblies "programatically" during runtime.... albert200 |
| albert200 |
Hmm... You should definitely be seeing a CLR-loaded error dialog in that case. What kind of application is this: console, WinForms, WPF, or something else?
As far as being able to enter your Main method (and thereby allow you to trap the exception) goes, are you calling anything from the potentially missing reference directly in your Main method? If so, move this into a separate private method in your Program class so that your Main code can run before the JIT compiler hits the missing reference problem. |
| Nicole Calinoiu |
My answer wasn't about programmatic loading either. You can use the event to help you detect a problem loading your reference. You don't need to supply a non-null return value if you don't have a substitute assembly available. |
| Nicole Calinoiu |
It'S a WinForms application albert200 |
| albert200 |
And does moving any use of types from the problem assembly out of the Main method help? |
| Nicole Calinoiu |
When removing all references to the assembly in query into separate private methods, I do get a .Net framework alert in release mode which indicates the exception being unhandled.... Fine so far...
Still struggling to catch the exception myself.... e.g. a try catch finally... construct on the top level of the Main seems to fail, meaning no excpetion getting caught....
albert200 |
| albert200 |
If you keep all references to the problem assembly out of the Main method, a top level try/catch in the Main method should work. e.g.:
private static void Main(string[] args)
{
try
{
Program.DoSomethingWithProblemAssembly();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
|
| Nicole Calinoiu |
Doing the same thing as you propose in my code with the following results: a) When running the application in debug mode remotly from the IDEI get my MessageBox b) IfI run the same binary (debug version ) directly, not from within the the IDE,I get the .Net framework MessageBox. c) If i run the releae version, directly,I get the .Net framework MessageBox, too.
I would like to see only my messagebox, in every case. albert200 |
| albert200 |
I suspect that your Main method is still not quite right since you should never see the CLR-generated error dialog if your code handles the exception completely. Could you please post a short but complete sample ( http://www.yoda.arachsys.com/csharp/complete.html) that demonstrates the behaviour you described in your last post? - Marked As Answer byalbert200 Tuesday, September 22, 2009 4:46 PM
-
|
| Nicole Calinoiu |
I found a way how to reliably catch an exception with my own exception handler. Problem seemed to be an already openedform , from which's paint method the missing assembly was called/used. Must have been some kind of default/standard forms exeption handler active here.
Now when trying to create an object instance from a type hosted by the assembly in question, before any forms get instantiated,I always catch the corresponding exception (IDE, Debug or Release).
Thanks for your help. The point about moving assembly access into private methods was the missing hint I needed to solve the problem. - Edited byalbert200 Tuesday, September 22, 2009 4:54 PMbad grammatic
-
|
| albert200 |