Alex Angas
Alex Angas

Reputation: 60027

How do I force where an assembly is loaded from?

I have developed a console utility that performs some operations against a server application. Due to the nature of the server app I'm working with, I need to execute this utility on the server.

The problem is that the utility is referencing a common DLL that has previously been deployed to the server's GAC. Since the common DLL's deployment, it has been updated and my utility relies on these updates. I am unable to update the DLL in the GAC due to company policy regarding deployment.

By default my utility will use the outdated DLL in the GAC. Is there a way I can force it to use the updated DLL (e.g. by specifying a path on the file system)?

Upvotes: 3

Views: 868

Answers (6)

SamDevx
SamDevx

Reputation: 2378

As foson suggests, use codebase in your console's config file. Example is shown below (change publicKeyToken, name, version and href appropriately).

<runtime> 
 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
  <dependentAssembly> 
     <assemblyIdentity name="myCommonDll" publicKeyToken="32ab4ba45e0a69a1" culture="neutral" />
     <codeBase version="2.0.0.0" href="file://C:\Users\djpiter\Documents/myCommon.dll"/>
  </dependentAssembly> 
 </assemblyBinding> 
</runtime> 

So that would be the easiest way to force CLR to redirect call to your common DLL from GAC to a version sitting at href location. Your updated common dll should be strong-named just like the one in the GAC with same name, culture, publickeytoken but a new version. The only change, therefore, is in console's config file. You don't need to change reference in console's source code. You can simply keep using existing console app.

Upvotes: 1

foson
foson

Reputation: 10227

Try using a <codebase> element in the app.config

Upvotes: 0

Megacan
Megacan

Reputation: 2518

The Assembly class has some methods to load assemblies from specific locations.

Assembly.LoadFrom has a few overloads

EDIT: There is a way to specify, via configuration file, where to look for specific assembly versions. I can't recall exactly.

Upvotes: 2

Dario Solera
Dario Solera

Reputation: 5804

You might want to take a look at the AppDomain.AssemblyResolve event.

EDIT: the event is only fired when regular assembly resolution fails, so it does not fit your needs.

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1063058

Unfortunately, GAC tends to play a trump card - but if you have changed the version, then the GAC resolve should fail (as long as you have "Specific Version" set to true in the IDE), allowing it to load the local version?

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1501153

Does the updated DLL not have a new version number? I would expect that if you force the reference to use the right version number, it should pick up the local one.

Upvotes: 3

Related Questions