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

时间:2013-06-04 14:24:39

标签: c# .net app-config base-class-library

我想知道为什么nuget在安装app.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>

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

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

the version of my dll

为什么我需要这个bindingRedirect

2 个答案:

答案 0 :(得分:41)

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

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

答案 1 :(得分:8)

您只是在说明 0.0.0.0到2.5.19.0 之间的旧版本时,请将该版本替换为2.5.19.0的新版本

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

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