.Net Assembly Binding Redirect with Differing Public Key Tokens
Asked Answered
V

3

63

Is it possible to perform an assembly binding redirect between different versions of a referenced assembly if the public key token is null on the older version and set on the newer version?

For example, I have two assemblies...

System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

and

System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

Should the following assembly binding redirect work in the Asp.Net web.config...

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
      <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0"/>
    </dependentAssembly>
  </assemblyBinding>
</runtime>
Versus answered 3/2, 2010 at 10:43 Comment(0)
M
49

No, it is not possible. The assemblies need to have the same publicKeyToken.

The bindingRedirect tag only has the oldVersion and newVersion attributes, so there's no way to tell it about the "null" version anyway.

But the real reason behind is explained due to the strong name mechanism

Mandimandible answered 3/2, 2010 at 12:16 Comment(0)
P
7

You might be able to use the AppDomain.AssemblyResolve event to do that. I've included some sample code in this answer.

Precocity answered 26/2, 2010 at 20:48 Comment(2)
Sadly, it seems the public key tokens must still match. When I try to manually load a different assembly (with bindingRedirect or without), I get "The located assembly's manifest definition does not match the assembly reference."Unwarrantable
@ladenedge, if you look at the sample code in the answer I linked to, it lets you use any criteria you want to decide which assembly to use. In the sample, I used the assembly name and ignored the public key tokens. Note that my sample was dynamically loading an assembly from a file, not at launch time. To be fair, I haven't tried to do what you're talking about, so it may be impossible.Precocity
A
1

It does seem that a binding-redirect can't be used, but in my case I did manage to get around the problem with differing publicKeyTokens by amending the token-value being requested in the referencing DLL:

Disassemble the dll to IL, change reference, reassemble (and re-sign - which might be an issue if you don't have the keyfile).

(See my comment on when referencing assemblies, is it possible to insist on a version number but ignore the publickeytoken? (ie accept signed/unsigned) )

Airboat answered 29/10, 2014 at 15:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.