C# 为什么在添加 Microsoft.Bcl.Async 包后会在 app.config 文件中添加“bindingRedirect”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16920370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:07:14  来源:igfitidea点击:

Why is a "bindingRedirect" added to the app.config file after adding the Microsoft.Bcl.Async package?

c#.netapp-configbase-class-library

提问by GameScripting

I was wondering why nuget added the following code to my applications app.configfile, after installing the Microsoft.Bcl.Async:

我想知道为什么 nugetapp.config在安装后将以下代码添加到我的应用程序文件中Microsoft.Bcl.Async

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="System.Threading.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.5.19.0" newVersion="2.5.19.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

If I remove this XML-element from the config, the app will not work properly.

如果我从配置中删除此 XML 元素,应用程序将无法正常工作。

As far as I understand it, we can use the bindingRedirectto make the app load a newer or older version of an assembly in case the version we were using when compiling the EXE is gone.
However I am using exactly the version 2.5.19.0, why would I need a redirect then?

据我了解,bindingRedirect如果我们在编译 EXE 时使用的版本消失了,我们可以使用使应用程序加载新版本或旧版本的程序集。
但是我使用的正是版本2.5.19.0,那我为什么需要重定向呢?

the version of my dll

我的dll的版本

Whydo I need this bindingRedirect?

为什么我需要这个bindingRedirect

采纳答案by Richard Deeming

The assemblies Microsoft.Threading.Tasksand Microsoft.Threading.Tasks.Extensionsare still referencing v1.5.11.0 of System.Runtimeand System.Threading.Tasks.

程序集Microsoft.Threading.TasksMicrosoft.Threading.Tasks.Extensions仍在引用System.Runtime和 的v1.5.11.0 System.Threading.Tasks

Without the bindingRedirect, the Microsoft.*assemblies would try to load an old version of the System.*assemblies, which would fail.

如果没有bindingRedirectMicrosoft.*程序集将尝试加载旧版本的System.*程序集,这将失败。

回答by Bhargav Konda

You are simply saying whenever there is older version that is between 0.0.0.0 to 2.5.19.0, please replace that version with the new version that is 2.5.19.0

您只是说只要有介于0.0.0.0 到 2.5.19.0之间的旧版本,请将该版本替换为 2.5.19.0 的新版本

Let's say you don't have the older version available in your project and you are trying to access it, then you will end up with an error like "System.IO.FileLoadException: 'Could not load file or assembly"

假设您的项目中没有旧版本,并且您正在尝试访问它,那么最终会出现类似“System.IO.FileLoadException:'无法加载文件或程序集”的错误

So when your project is looking for an older version of that DLL it will simply replace that with new one which is available

因此,当您的项目正在寻找该 DLL 的旧版本时,它会简单地将其替换为可用的新版本