Assembly Binding redirect: How and Why?
Asked Answered
S

3

196

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>
    
Semeiology answered 12/4, 2017 at 9:34 Comment(3)
It can be any version, not just the major one. For example: oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0"Mckinney
@Evk: All the examples that I have see show major version only.Semeiology
Well those are just examples, and it is nowhere stated that it's the only way possible.Mckinney
M
270

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.

Mckinney answered 12/4, 2017 at 9:52 Comment(20)
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.Kremer
Valid questions @tpartee, I've edited the answer (awaiting peer review) to include the config section and a link to learn.microsoft.com/en-us/dotnet/framework/configure-apps/…Silhouette
app.config or web.config: ` <configuration> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="C" publicKeyToken="32ab4ba45e0a69a1" culture="en-us" /> <bindingRedirect oldVersion="1.1.1.0" newVersion="1.1.2.5" /> </dependentAssembly> </assemblyBinding> </runtime> </configuration> `Silhouette
@Kremer 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.Mckinney
@Mckinney So should the binding redirects be put in config file of application A or in that of library B?Lenis
@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.Mckinney
@Mckinney I think they do have some effect as they're often automatically added in class library projects too, trying to figure that out :-)Lenis
@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://mcmap.net/q/130026/-do-binding-redirects-in-app-config-for-class-libraries-do-anything/5311735Mckinney
@Mckinney Thanks for the link! This is exactly my question, favorited until there's an answerLenis
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!Gallaway
@LeMotJuiced so what are you trying to implement exactly? If you are searching for "newVersion" I assume you want to remove some binding redirects?Mckinney
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.Gallaway
@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.Mckinney
@Mckinney 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.Excoriate
@Excoriate 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://mcmap.net/q/110619/-getting-the-publickeytoken-of-net-assemblies-duplicate/5311735Mckinney
Does publicKeyToken refer to the new or old C DLL?Gujral
An important notice (which I did know before). According to this answer, both assemblies (old and new) must have the same PublicKeyToken, otherwise the redirection will not work.Gujral
@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.Mckinney
You cannot load different versions of the same assembly at runtime - why not fix this instead of all this redirect rubbishHartford
@PaulMcCarthy I think .NET team knows it's rubbish. Thankfully they didn't bring this "feature" to .NET Core. nickcraver.com/blog/2020/02/11/binding-redirectsSybarite
B
118

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

Belak answered 7/8, 2018 at 22:10 Comment(0)
P
1

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

Polyptych answered 7/3, 2023 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.