30

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?

Community
  • 1
  • 1
Michael Stum
  • 177,530
  • 117
  • 400
  • 535

5 Answers5

15

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.

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
  • 5
    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. – Sam Mar 06 '12 at 17:58
  • 1
    @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 – Eonasdan Jun 11 '12 at 15:39
  • @Eonasdan how did he do what? If you mean, how to use sn.exe, you can refer to this MSDN article: http://msdn.microsoft.com/en-us/library/k5b5tt23.aspx – Andrew Savinykh Jun 12 '12 at 00:14
  • 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.) – jpmc26 Mar 01 '16 at 22:51
  • @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. – Andrew Savinykh Mar 02 '16 at 09:05
14

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.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
6

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

Wolf5
  • 16,600
  • 12
  • 59
  • 58
  • Can you briefly explain how to do that? Thanks –  Sep 17 '16 at 14:20
  • 1
    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. – Wolf5 Sep 18 '16 at 13:55
  • 1
    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 –  Sep 19 '16 at 08:33
6

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

Sven Sowa
  • 337
  • 3
  • 5
  • 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? – cangosta Jul 06 '16 at 14:54
  • but it doesn't work for mscorlib, does it? or is there a trick to load also mscorlib from outside the GAC? –  Sep 17 '16 at 15:35
0

You'd have to do something like manually loading the assembly from the file (Assembly.LoadFrom, or Assembly.Load(byte[]) ) before it is loaded from the GAC, and then handle the AppDomain.AssemblyResolve event. Check out Suzanne Cook's blog post for more details.

SharpC
  • 6,974
  • 4
  • 45
  • 40
erikkallen
  • 33,800
  • 13
  • 85
  • 120