C#: Problem/Solution: AssemblyResolve event not hit (used to load an assembly from some other folder other than the bin folder. )


AssemblyResolve event not hit

I have an app that depends on some assemblies stored in a subfolder. I’d like to get rid of the app.config file (and the privatePath="MySubFolder" within) and I don’t want to install the assemblies into the GAC.

What I’m trying to do is to use the AssemblyResolve event to tell the app where to look for the assemblies. However, when I run the app I get a FileNotFoundException before the execution hits the AssemblyResolve handler. In fact I get that error before anything else happens. I placed a breakpoint on the entry point of the app and the exception comes before the very first line of code is executed.

This is the same with both .NET 3.5 and 4.0.

Any advice is highly appreciated.

Solution 4


Hi there.
I’ve just checked the code and tested, and came out with a solution. The problem is that the library needs to be loaded somewhere before it’s used. But if you use it in the Main then it should be loaded before the Main ( = before adding the AppDomain.CurrentDomain.AssemblyResolve event handler). All you have to do is move your code in a separate procedure cleaning your Main from all code that uses the ClientClass, then call that procedure in the Main, but not before creating the event handler, just like this:

Collapse | Copy Code

static void Main(string[] args)
 AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
static void UseClientClass()
 Console.Write("Enter name: ");
 string name = Console.ReadLine();
 ClientClass.ClientClass clientClass = new ClientClass.ClientClass();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s