How to catch System.IO.FileLoadException when starting application

J

jeeji

Hi

I have a console application that in some cases could get a
System.IO.FileLoadException
Could not load file or assembly XXX, Version=5.0.0.0, Culture=neutral,
PublicKeyToken=YYYY' or one of its dependencies. The located
assembly's manifest definition does not match the assembly reference.
(Exception from HRESULT: 0x80131040)

However, I cannot catch this exception, since it is thrown before I
reach Main() in my application. Is there a way to catch it, and by
that give a nicer message to the console, and also exit with a
relevant exit code.


Note that I know why it is throwing this exception. So all I need is a
way to handle it, and by handle I mean give a more meaningful message
to the console, and exit with code 1.

Thanks
Jeeji
 
C

ClayB

Try subscribing to the static event AssemblyResolve to see if that
will allow you to catch this exception.

AppDomain.CurrentDomain.AssemblyResolve += new
ResolveEventHandler(AssemblyResolver);


public static System.Reflection.Assembly AssemblyResolver(object
sender, ResolveEventArgs e)
{
// return the proper assembly
}

========================
Clay Burch
Syncfusion, Inc.
 
A

Amar

Hi Jeeji..
You have two options..
Both should work...
1> Subscribe to the Application.ThreadException event.
2>Subscribe to the AppDomain.CurrentDomain.UnhandledException event.

I have used both these methods.. but havent tried it whenan exception
is thrown before the main method is hit..
Hope this helps...
 
J

jeeji

Hi and thanks for your fine answers.

But I do not understand what you mean.
Where should I subscribe to those events.

I doubt that I can add them in my main function. And if the exception
is thrown before I enter my main function, then it makes no sense,
or...?

Is it not possible to delay the loading of the dll until I access the
classes I need in those dlls? Can I not set an option on the dll, to
say "load first when needed"?

Jeeji
 
A

AvdP

Is it not possible to delay the loading of the dll until I access the
classes I need in those dlls? Can I not set an option on the dll, to
say "load first when needed"?

With an assembly (DLL) that is composed of one or more netmodules this is
possible (Google for "common language runtime netmodule" or "clr netmodule"
to get more info). But I don't know if it will solve the exception.

Regards,
Anne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top