【问题标题】:Assembly binding redirect does not work程序集绑定重定向不起作用
【发布时间】:2011-03-30 06:04:36
【问题描述】:

我正在尝试使用以下 app.config 设置程序集绑定重定向:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.AnalysisServices"
                          PublicKeyToken="89845dcd8080cc91" />
        <bindingRedirect oldVersion="10.0.0.0"
                         newVersion="9.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

我在 GAC 中版本为 9.0.242.0 的机器上运行该程序,并使用指定的公钥令牌。不过,CLR 似乎甚至没有尝试重定向绑定以使用该版本。

这是我在 fuslogvw.exe 中得到的:

LOG: This bind starts in default load context. LOG: Using application configuration file: \Debug\AssemblyRedirectPOC.exe.Config LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config. LOG: Post-policy reference: Microsoft.AnalysisServices, Version=10.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91 LOG: GAC Lookup was unsuccessful. LOG: Attempting download of new URL /Debug/Microsoft.AnalysisServices.DLL. LOG: Attempting download of new URL /Debug/Microsoft.AnalysisServices/Microsoft.AnalysisServices.DLL. LOG: Attempting download of new URL /Debug/Microsoft.AnalysisServices.EXE. LOG: Attempting download of new URL /Debug/Microsoft.AnalysisServices/Microsoft.AnalysisServices.EXE. LOG: All probing URLs attempted and failed.

当我尝试将 9.0.242.0 版本的 dll 放入探测路径时,我得到了这个:

LOG: Assembly download was successful. Attempting setup of file: \Debug\Microsoft.AnalysisServices.dll LOG: Entering run-from-source setup phase. LOG: Assembly Name is: Microsoft.AnalysisServices, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91 WRN: Comparing the assembly name resulted in the mismatch: Major Version ERR: The assembly reference did not match the assembly definition found. ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

请注意,我还尝试将重定向更改为在 app.config 中使用“9.0.242.0”而不是“9.0.0.0”,但这没有用,尽管我认为这不会有任何区别。

据我了解,重定向绑定的全部意义在于使用与构建程序的版本不匹配的版本。我在这里完全错过了什么吗?我正在尝试做的事情是否可行,如果可以,知道为什么它不起作用吗?

干杯, 亚当

【问题讨论】:

    标签: c# .net binding assemblies reference


    【解决方案1】:

    配置 xml 中的任何拼写错误都可能是一个原因。加载器只是看不到您的配置。 我也头疼了一个小时,直到我意识到错误出现在模式名称中的字符“=”而不是“-”中:

    <assemblyBinding xmlns="urn:schemas=microsoft-com:asm.v1">
    

    只需仔细检查所有属性名称和值。我猜“PublicKeyToken”应该是“publicKeyToken”

    这应该可行:

    <configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Microsoft.AnalysisServices" publicKeyToken="89845dcd8080cc91" />
                <bindingRedirect oldVersion="10.0.0.0" newVersion="9.0.0.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
    </configuration>
    

    【讨论】:

    • 添加 xmlns="urn:schemas=microsoft-com:asm.v1" 为我解决了这个问题。谢谢!
    • 错误的(用 = 代替 -)对你有用
    • 一个常见的错误是制作糟糕的 XML cmets。例如,如果您添加 --(双连字符)inside 注释而不是仅在结束标记处,则无法读取整个 XML。
    • 这对于初学者来说可能值得注意,请确保依赖项位于 Assemblies 文件夹中
    【解决方案2】:

    确保您的&lt;configuration&gt; 标记没有命名空间属性。否则任何&lt;assemblyBinding&gt; 标签都会被忽略。

    错误:

    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
    

    对:

    <configuration>
    

    (来自https://stackoverflow.com/a/12011221/150370

    【讨论】:

    • 就是答案。绑定重定向被忽略了,因为我的 web.config 太旧了,它有 2.0 命名空间。
    • 2 小时的头痛,然后解决方法就像移除 ns 一样简单。
    【解决方案3】:

    我遇到程序集绑定重定向不起作用,因为 assemblyBinding 元素上缺少命名空间。

    正确

    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="TIBCO.Rendezvous" publicKeyToken="1a696d1f90f6158a"/>
        <bindingRedirect oldVersion="1.0.0.0-1.0.3191.28836" newVersion="1.0.3191.28836"/>
      </dependentAssembly>
    

    不正确

    注意缺失:xmlns="urn:schemas-microsoft-com:asm.v1"

    <assemblyBinding>
      <dependentAssembly>
        <assemblyIdentity name="TIBCO.Rendezvous" publicKeyToken="1a696d1f90f6158a"/>
        <bindingRedirect oldVersion="1.0.0.0-1.0.3191.28836" newVersion="1.0.3191.28836"/>
      </dependentAssembly>
    

    【讨论】:

      【解决方案4】:

      在我的情况下,我必须删除

      appliesTo="v2.0.05727" 
      

      来自

      <assemblyBinding appliesTo="v2.0.05727" xmlns="urn:schemas-microsoft-com:asm.v1">
      

      【讨论】:

        【解决方案5】:

        我遇到过类似的问题,将绑定重定向移动到 Machine.Config 是唯一有效的方法。这在我的 winform 应用中不是理想的解决方案,因为我将我的应用分发给客户。

        解决方案:

        确保 .config 文件位于运行应用程序的目录中。例如如果您的 AppName 是“MyApp”,那么重定向应该在应用程序目录中的“MyApp.exe.Config”文件中。

        即使使用第三方 dll 的代码在我的解决方案中位于不同的 dll 中并且添加 .dll.config 没有帮助,我也必须这样做。

        【讨论】:

        • Moving bindingredirects to Machine.Config 解决了我的问题。
        【解决方案6】:

        当我将绑定重定向配置移动到 machine.config 文件时,我的问题得到了解决。

        【讨论】:

        • 将绑定重定向到 Machine.Config 解决了我的问题
        • 尝试在 web.config 中将 部分移到更高的位置。从 下的右侧开始。比 machine.config 好多了!
        【解决方案7】:

        如果对任何人有任何帮助,我遇到了这个问题,因为我没有将完整版本放入 newVersion。即,我有 newVersion="3.0.1" 而不是 newVersion="3.0.1.0"

        【讨论】:

          【解决方案8】:

          为那些也在苦苦挣扎的人提供一个额外的解决方案

          确保每个&lt;dependentAssembly&gt; 只有一个&lt;assemblyIdentity&gt; 和一个&lt;bindingRedirect&gt;。在我的场景中,我有二合一,这导致多个绑定重定向的级联失败

          <dependentAssembly>
                  <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                  <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
          
                  <assemblyIdentity name="SimpleInjector" publicKeyToken="984cb50dea722e99" culture="neutral" />
                  <bindingRedirect oldVersion="0.0.0.0-4.4.2.0" newVersion="4.4.2.0" />
          </dependentAssembly>
          

          这意味着我的 SimpleInjector 不是绑定到 4.4.2.0,而是绑定到 5.2.3.0,这导致错误告诉我它无法正确绑定 System.Web.Mvc,从而掩盖了真正的问题

          【讨论】:

          • 谢谢,这真的很有帮助!似乎由于这个问题,部分或全部重定向被忽略了。
          【解决方案9】:

          非常感谢您的回答,尤其是来自 Shrike 的回答。我有一个在开发中工作的应用程序,但在部署版本中没有。当我仔细观察时,我发现这是在生产中,与开发不匹配:

            <runtime>
              <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                <dependentAssembly xmlns="">
                  <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
                  <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
                </dependentAssembly>
              </assemblyBinding>
            </runtime>
          

          dependentAssembly xmlns="" 是罪魁祸首。一旦我将我的答案与您的答案进行比较并修复它,它就可以工作了。感谢您的帮助

          【讨论】:

            【解决方案10】:

            古怪的密码策略也可能导致配置中的 assemblyBinding 项被忽略。配置文件中显然不允许使用 '&' 和 '^' 之类的字符。 Notepad++ 中的 XML 工具在我对 Assembly Binding Log Viewer 进行了几个小时的摆弄后向我揭示了这一点。

            【讨论】:

              【解决方案11】:

              如果您在没有 ASP.NET 开发工具部分的情况下安装 Visual Studio 2017,它仍会加载 Web 项目并编译和构建它。它只会给出有关 NuGet 包版本的警告,因为它不知道如何处理 web.config 文件,因此看不到绑定重定向。

              修复安装解决了我的问题,但花了很长时间才弄清楚。

              【讨论】:

              • 谢谢!这发生在我从 VS2015 升级到 VS2017 的过程中。没有错误 - 只是不会发布:( :( :(
              【解决方案12】:

              在我的例子中,app.config 文件没有被构建后脚本复制到目标目录。我为配置文件创建了异常,以便不覆盖目标中的配置 - 我完全忘记了它。所以目标目录中的应用程序正在使用旧的 app.config 文件。

              【讨论】:

              • 澄清一下,对我来说,需要将 (appName).exe.config 文件与主 (appName).exe 文件一起复制。
              【解决方案13】:

              检查是否有错误 xxx , Culture=neutral, PublicKeyToken= xxx" 上的显式绑定重定向与自动生成的绑定重定向冲突

              出现在输出窗口中(不会出现在错误窗口中)

              【讨论】:

                【解决方案14】:

                我将身份模拟设置为 true,将其更改为 false 并为我工作

                【讨论】:

                • 嗨,欢迎来到 StackOverflow!请考虑在您的答案中添加一些解释和详细信息。
                【解决方案15】:

                好吧,就我而言,dependentAssembly 节点位于 assemblyBinding 节点之外。错误的复制粘贴。

                  <runtime>
                    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                      <dependentAssembly>
                        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
                        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
                      </dependentAssembly>
                    </assemblyBinding>    
                    <dependentAssembly>
                      <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
                      <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
                    </dependentAssembly>
                
                  </runtime>
                

                【讨论】:

                  【解决方案16】:

                  即使culture="neutral"(或在您的情况下的任何值)没有出现在此处和其他地方的某些答案中,在我的情况下它是最重要的:当我添加它时,一切都很顺利。

                  发帖人从来没有说他的问题是什么,所以也试试这个。

                  【讨论】:

                    【解决方案17】:

                    我正在尝试提供一份清单以及解决问题的工具。

                    1. 应正确定位目标newVersion 程序集。使用 Sysinternals(link) 的“进程监视器”工具来跟踪程序集的搜索方式
                    2. 检查目标程序集的版本是如何解决的。按照this帖子中的步骤启用Fusion(.NET Framework的程序集加载器)的日志输出,然后检查具有目标程序集名称的文件
                    3. 注意Fusion在解析目标程序集时读取的配置文件的名称。

                    【讨论】:

                      猜你喜欢
                      • 2012-09-01
                      • 1970-01-01
                      • 2012-04-27
                      • 2017-02-16
                      • 2017-09-09
                      • 2019-12-21
                      • 1970-01-01
                      • 1970-01-01
                      相关资源
                      最近更新 更多