程序集绑定重定向不起作用

问题描述 投票:28回答:11

我正在尝试使用以下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
11个回答
26
投票

配置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>

0
投票

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

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


0
投票

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

  <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 =“”是罪魁祸首。一旦我将我的答案与你的答案进行比较,并解决了这个问题,它就有效了。谢谢您的帮助


0
投票

对于那些也在努力的人来说,还有一个额外的解

确保每个<dependentAssembly>只有一个<assemblyIdentity>和一个<bindingRedirect>。在我的场景中,我有两个一个,这导致多个绑定重定向的级联失败

<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,掩盖真正的问题


16
投票

确保您的<configuration>标记没有名称空间属性。否则任何<assemblyBinding>标签都将被忽略。

错误:

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

对:

<configuration>

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


11
投票

我遇到程序集绑定重定向不起作用,因为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>


5
投票

在我的情况下,我不得不删除

appliesTo="v2.0.05727" 

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

3
投票

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


3
投票

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

解:

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

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


1
投票

偏心密码策略还可以导致忽略配置中的assemblyBinding项。配置文件中显然不允许使用“&”和“^”等字符。 Notepad ++中的XML工具在使用程序集绑定日志查看器进行了几个小时的调整后向我展示了这一点。


1
投票

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

修复安装修复了我的问题,但它花了很长时间才弄明白。

Visual Studio Installer screenshot


0
投票

如果它对任何人有任何帮助,我遇到了这个,因为我没有把完整版本用于newVersion。即,我有newVersion="3.0.1"而不是newVersion="3.0.1.0"

© www.soinside.com 2019 - 2024. All rights reserved.