Force load an assembly from the /bin and not the GAC?

20,894

Solution 1

GAC is always tried first, when binding assemblies: How the Runtime Locates Assemblies

So no, you can't do this. However if you explain why you have such strange requirements there might be a different work around, you have not thought of.

Solution 2

No there is no way to do this. When loading an assembly the CLR will check to see if a DLL with an equivalent strong name is present in the GAC. If there is a matching assembly in the GAC it will pick the GAC assembly every time. There is unfortunately no way to override this behavior.

Solution 3

It is possible, but it is advanced and require your knownledge on CLR and how it works and C++.

Take a look at this google book:Customizing the Microsoft® .NET Framework Common Language Runtime

Keywords: IHostAssemblyManager, IHostAssemblyStore

Solution 4

I found another way. It is only meant for developers, but it works. According to https://msdn.microsoft.com/en-us/library/cskzh7h6(v=vs.100).aspx you set this in the your .config file

<configuration>
  <runtime>
    <developmentMode developerInstallation="true"/>
  </runtime>
</configuration>

You also need to set the Environment variable DEVPATH with to the path of your dll. Open a cmd, set the variable, run your app:

SET DEVPATH=YOURLOCALPATH

This helped me to load a local Oracle.ManagedDataAccess.dll since Oracle releases new versions, but they all have the same Version and PublicKeyToken. Thanks Oracle!

You can use Process Explorer from Microsoft to see the difference. See https://technet.microsoft.com/en-us/sysinternals/ See this as a small proof of concept: Process Explorer screenshot

Share:
20,894
Michael Stum
Author by

Michael Stum

The same thing we do every night, Pinky. Try to take over the world! Full-Stack Developer on Stack Overflow Enterprise, working to make our little corner of the Internet better for all of us.

Updated on November 11, 2020

Comments

  • Michael Stum
    Michael Stum over 3 years

    I have two assemblies, created through conditional compilation (dev and real).

    The public surface of these assemblies is 100% identical: both are strongly named; both are signed with the same .snk and therefore have the same PublicKeyToken; both have the same culture and the same version. I cannot change this: making them appear identical is the whole point.

    However, on my machine the real assembly is in the GAC. I have an ASP.NET 3.5 WebForms app that references the dev assembly. It absolutely must do that; the real assembly crashes the app.

    Is there a way to force a specific ASP.NET application to use the dev one (which is in /bin), given that:

    • There is one in the GAC.
    • Both have the same Version and PublicKeyToken.
    • Both are strongly named/signed with the same key.
    • I can not change them, can't change the version, and can't remove the key.

    I noticed that someone already asked this in #991293, but the accepted answer involved removing the signing, which isn't an option here.

    Am I out of luck?

  • JaredPar
    JaredPar almost 13 years
    Even Assembly.LoadFrom won't work. The CLR will ignore your path and pick the GAC.
  • Sam
    Sam about 12 years
    I know this is old, but for example: A shared host might have an assembly installed into the GAC. The assembly is open source, I've fixed a bug critical to my app, compiled it, and put it in my bin folder. What I ended up doing was self-signing the code and using sn.exe to get the new public token.
  • Eonasdan
    Eonasdan almost 12 years
    @sam how did you do this? I want to do this for the system.web.http since my webhost has the mvc 4 beta in the gac but I want to use the new RC
  • Andrew Savinykh
    Andrew Savinykh almost 12 years
    @Eonasdan how did he do what? If you mean, how to use sn.exe, you can refer to this MSDN article: msdn.microsoft.com/en-us/library/k5b5tt23.aspx
  • erikkallen
    erikkallen about 11 years
    Not even if pre-loaded in a module initializer?
  • jpmc26
    jpmc26 about 8 years
    I have these "strange requirements" because Microsoft made two different versions of an assembly with identical version numbers. (I can tell they are different from the errors I get and from the file hashes.)
  • Andrew Savinykh
    Andrew Savinykh about 8 years
    @jpmc26, do tell! Preferably in a separate question with all the relevant details, such as assemblies name and version, where did you get different versions from and all these errors and hashes you are observing.
  • cangosta
    cangosta almost 8 years
    This works fine for development env as you said. But how do you deal with this problem when you have your application spread out in thousands of users' machine?
  • Admin
    Admin over 7 years
    Can you briefly explain how to do that? Thanks
  • Admin
    Admin over 7 years
    but it doesn't work for mscorlib, does it? or is there a trick to load also mscorlib from outside the GAC?
  • Wolf5
    Wolf5 over 7 years
    I couldn't as it is not briefly explained. It is why I added the keywords to look up. It requires you to override the default way for CLR to load assemblies.
  • Admin
    Admin over 7 years
    so yours is more a comment than an answer... anyway I was trying to understand if it is applicable to mscorlib, but I believe it isn't
  • Himalaya Garg
    Himalaya Garg about 5 years
    What does 'Specific Version' = true do then?
  • 4thex
    4thex over 4 years
    Specific Version for a reference in Visual Studio is only for validation during compilation.