184

This is not a problem question but a general understanding question on assembly binding redirect's working.

Queries

  1. Why binding redirect shows only major version and not minor, build and revision numbers?
  2. Does old and new version change only when there is change in major version?

    <dependentAssembly>
        <assemblyIdentity name="FooBar"  
                          publicKeyToken="32ab4ba45e0a69a1"  
                          culture="en-us" />  
    
        <bindingRedirect oldVersion="7.0.0.0" newVersion="8.0.0.0" />  
    </dependentAssembly>
    
Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208

3 Answers3

259

Why are binding redirects needed at all? Suppose you have application A that references library B, and also library C of version 1.1.2.5. Library B in turn also references library C, but of version 1.1.1.0. Now we have a conflict, because you cannot load different versions of the same assembly at runtime. To resolve this conflict you might use binding redirect, usually to the new version (but can be to the old too). You do that by adding the following to app.config file of application A, under configuration > runtime > assemblyBinding section (see here for an example of full config file):

<dependentAssembly>
    <assemblyIdentity name="C"  
                      publicKeyToken="32ab4ba45e0a69a1"  
                      culture="en-us" />  

    <bindingRedirect oldVersion="1.1.1.0" newVersion="1.1.2.5" />  
</dependentAssembly>

You can also specify a range of versions to map:

<bindingRedirect oldVersion="0.0.0.0-1.1.1.0" newVersion="1.1.2.5" />  

Now library B, which was compiled with reference to C of version 1.1.1.0 will use C of version 1.1.2.5 at runtime. Of course, you better ensure that library C is backwards compatible or this might lead to unexpected results.

You can redirect any versions of libraries, not just major ones.

Little Endian
  • 784
  • 8
  • 19
Evk
  • 98,527
  • 8
  • 141
  • 191
  • What file and under what section do these go into? Can someone please provide a link to source like MSDN or similar for reference? Remember people will be landing on your SO Q/A articles from all over the search engine sphere and references are critical. I had a coworker tell me to "just add an assembly redirect to your exe file" right before going on vacation for a week and I landed here and while this answer looks great it's lacking context and reference. – tpartee Aug 24 '17 at 18:40
  • Valid questions @tpartee, I've edited the answer (awaiting peer review) to include the config section and a link to https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/redirect-assembly-versions – Kobus Smit Sep 27 '17 at 01:17
  • app.config or web.config: ` ` – Kobus Smit Sep 27 '17 at 01:18
  • @tpartee sorry I somehow missed all those comments. Hopefully you figured it out already. Answer does not include this because question already assumes author knows such details. – Evk Nov 15 '17 at 15:03
  • @Evk So should the binding redirects be put in config file of application A or in that of library B? – Alexander Derck Feb 15 '18 at 10:09
  • 2
    @AlexanderDerck in config file of application A - they have no effect (as far as I'm aware) in config files of libraries, except maybe when this library is unit test library and is "executed" in some sense by unit test runner. – Evk Feb 15 '18 at 10:14
  • @Evk I think they do have some effect as they're often automatically added in class library projects too, trying to figure that out :-) – Alexander Derck Feb 15 '18 at 10:21
  • 1
    @AlexanderDerck there was a question couple of weeks ago, with many upvotes and even bounty, which was asking exactly that, but no one was able to provide convincing answer - https://stackoverflow.com/q/48377474/5311735 – Evk Feb 15 '18 at 10:26
  • @Evk Thanks for the link! This is exactly my question, favorited until there's an answer – Alexander Derck Feb 15 '18 at 10:30
  • This seems like a helpful answer but I have no idea how to implement it. Doing a project search for "newVersion" comes up with zero results! – Le Mot Juiced Apr 23 '18 at 10:15
  • @LeMotJuiced so what are you trying to implement exactly? If you are searching for "newVersion" I assume you want to remove some binding redirects? – Evk Apr 23 '18 at 10:18
  • I'm just searching for "newVersion" because I see that word in the error message and I've been pulling my hair out on this for days and I can't think of anything else to do. I've been told that the thing about inserting code at a certain node should be ignored. Nobody seems to have an answer for this. – Le Mot Juiced Apr 23 '18 at 13:55
  • @LeMotJuiced well maybe you should create a new question for that (or point me to one if you already asked), since "I see that word in error message" is quite vague description, and in question you can describe your problem in a more complete way. – Evk Apr 23 '18 at 13:58
  • @Evk does the "publicKeyToken" attribute comes from project X libracy C 1.1.2.5 binding redirect on the app.config of X project ? How would I generate this for adding that assembly redirect on my project A ?. Thanks in Advance. – CodeEngine Jun 24 '19 at 12:55
  • 1
    @CodeEngine publicKeyToken identifies assembly C. Only signed assemblies have that public key token identifying them. Here is a related question about how you can find out that token given that you have assembly: https://stackoverflow.com/q/3045033/5311735 – Evk Jun 25 '19 at 10:03
  • Does `publicKeyToken` refer to the new or old C DLL? – Mohammed Noureldin Sep 26 '19 at 10:54
  • 1
    An important notice (which I did know before). According to [this answer](https://stackoverflow.com/questions/2191296/net-assembly-binding-redirect-with-differing-public-key-tokens), both assemblies (old and new) must have the same PublicKeyToken, otherwise the redirection will not work. – Mohammed Noureldin Sep 26 '19 at 11:06
  • @MohammedNoureldin sure, publik key token is what identifies given assembly. If old and new assemblies have different token - they are different assemblies, and redirect in this way is not possible. – Evk Sep 26 '19 at 11:10
  • 1
    You cannot load different versions of the same assembly at runtime - why not fix this instead of all this redirect rubbish – Paul McCarthy Oct 04 '19 at 15:24
  • 2
    @PaulMcCarthy I think .NET team knows it's rubbish. Thankfully they didn't bring this "feature" to .NET Core. https://nickcraver.com/blog/2020/02/11/binding-redirects/ – Mariusz Pawelski Oct 27 '20 at 12:02
109

We came across an issue with binding redirect for NewtonSoft.Json. We looked up the file version in win 10 file properties "9.0.1.19813", looked up the number and the redirect kept failing. Further investigation and found that we were looking at file version and not assembly version. So, I wonder if people are mistaking File Version (which changes often) and Assembly version (which you can't see in windows 10 File Explorer). To see the Assembly version of a dll you can run this in powershell. Replace the dll name with the one you want to find version for.

[Reflection.AssemblyName]::GetAssemblyName('C:\development\bin\Newtonsoft.Json.dll').Version

The result of above is.

Major  Minor  Build  Revision

-----  -----  -----  --------

9      0      0      0

See References:

How can i see the assembly version of a .NET assembly in Windows Vista and newer (WIndows 7, 2008)?

https://support.microsoft.com/en-nz/help/556041

enter image description here

Amyth
  • 1,367
  • 1
  • 9
  • 15
0

I highly recommend to use dotPeek from JetBrains to look at dll library and it's version. According your second question. Versions can be absolutely different, not only major versions count. enter image description here