Groups | Blog | Home
all groups > dotnet clr > august 2004 >

dotnet clr : Assembly.Load() with alternate subdirectory for other assemblies which have dependencies



jcwiese NO[at]SPAM yahoo.com
8/31/2004 10:00:55 AM
I have a question about dependent assemblies of asseblies that are
loaded with Assembly.Load(). I have a directory structure with the
following format:

C:\temp\
-> myApp.exe
-> OurCompany.Common.dll (version 1.5.0.0 with common routines,
strongly signed)

C:\temp\ExternalLibrary
-> ExternalLibrary.dll
-> OurCompany.Common.dll (version 1.2.0.0 with common routines,
strongly signed)


From "myApp.exe", the following code is used:

Assembly loaded = Assembly.Load( "ExternalLibrary" ) ;
object classToUse =
loaded.CreateInstance("ClassInsideExternalLibrary") ;

.... the loading of the assembly seems to work just fine, but when
CreateInstance() is called, it fails because it can not find
OurCompany.Common.dll with version 1.2, which is a dependency in
ExternalLibrary.dll's manifest. From my understanding, it should have
been loaded since it resides inside the Load() context. Is this
correct? If not, any suggestions of how to make this work without the
GAC would be appreciated. I can temporarily resolve this issue by
using a <bindingRedirect> in the myApp.exe.config file. This is much
less usefull as it makes a central location with all of the detailed
knowledge of each of the loaded assemblies.

Christian Heide Damm
9/2/2004 3:44:00 PM
One solution would be to load OurCompany.Common.dll v1.2.0.0 _before_ you
load ExternalLibrary.dll - that way it doesn't have to look for it when it
needs it.

Another way would be to subscribe to the AppDomain.AssemblyResolve event.
When that event is raised for the "OurCompany.Common.dll, version 1.2.0.0,
...." assembly, you load it from c:\ExternalLibrary. This is the lazy variant
of the first solution.


Reg. the question about load contexts...

First of all, I don't understand how you can load ExternalLibrary.dll by
Assembly.Load("ExternalLibrary"). I would think you'd need to say

AssemblyName externalLibraryName = new AssemblyName("ExternalLibrary");
// include version etc. if you want
externalLibraryName.Location = @"c:\temp\ExternalLibrary";
Assembly.Load(externalLibraryName);

Anyway, it looks like it's loaded in the LoadFrom context - in which case I
would actually expect it to find ExternalLibrary.dll v1.2.0.0 because it is
in the same directory. If it is in fact loaded in the Load context, I would
NOT expect it to find ExternalLibrary.dll v1.2.0.0.


Christian

---

[quoted text, click to view]

David Levine
9/3/2004 5:19:54 AM
Before you load any assembly with a dependency on the common.dll or loading
it directly, call
AppDomain.CurrentDomain.AppendPrivatePath("ExternalLibrary");

This tells the runtime where it can look below the application base
directory for other assemblies, and avoids needing to explicity load the
assembly using LoadFrom.


[quoted text, click to view]

AddThis Social Bookmark Button