ChatGPT解决这个技术问题 Extra ChatGPT

Assembly Binding redirect: How and Why?

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

Queries

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

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"
@Evk: All the examples that I have see show major version only.
Well those are just examples, and it is nowhere stated that it's the only way possible.

L
Little Endian

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.


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.
@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.
@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 - stackoverflow.com/q/48377474/5311735
@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: stackoverflow.com/q/3045033/5311735
@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-redirects
A
Amyth

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

https://i.stack.imgur.com/jR6cv.jpg