无法加载文件或程序集"WebGrease"的其中一个依赖项。所定位的程序集清单定义与程序集引用不匹配。

20

这个问题有很多解决方案,请阅读下面的所有答案,它们可能会帮助您解决问题。如果您找到了新的解决方法,请在您的答案中记录

我正在尝试将System.Web.Optimization添加到我的ASP.NET Web Forms解决方案中。 我通过NuGet Packages添加了Microsoft ASP.NET Web Optimization Framework。它将Microsoft.Web.Infrastracture和WebGrease(1.5.2)添加到了引用中。

然而,当我运行时

<%= System.Web.Optimization.Scripts.Render("~/bundles/js")%>

我遇到了运行时错误

Could not load file or assembly 'WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

我尝试将 assemblyBinding 添加到 Web.Config 中。

<runtime>
  <legacyUnhandledExceptionPolicy enabled="1"/>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.5.1.25624" newVersion="1.5.2.14234"/>
      </dependentAssembly>
    </assemblyBinding>
</runtime>

但是没有任何好运。

我注意到我的网站的Web config文件包含这一行:

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

替换它。
 <configuration>

然后一切正常,我没有得到运行时错误。不幸的是,我需要xmlns。我的项目的其他组件依赖于它。

为什么Optimization会尝试加载旧版本,而schema指向v2.0?有没有办法强制它加载最新或唯一可用的WebGrease.dll?

在不更改的情况下还能尝试什么?

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

非常感谢您所提供的任何帮助!

编辑: 1)附上了FusionLog结果。也许会有所帮助。

=== Pre-bind state information ===
LOG: User = [USER]
LOG: DisplayName = WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35
(Fully-specified)
LOG: Appbase = file:///C:/Projects/PROJECT_NAME/trunk/www.PROJECT_NAME.com/
LOG: Initial PrivatePath = C:\Projects\PROJECT_NAME\trunk\www.PROJECT_NAME.com\bin
Calling assembly : System.Web.Optimization, Version=1.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Projects\PROJECT_NAME\trunk\www.PROJECT_NAME.com\web.config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: WebGrease, Version=1.5.1.25624, Culture=neutral, PublicKeyToken=31bf3856ad364e35

2) 确认,问题出在

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

然而,我不明白为什么

8个回答

29

我在生产服务器上遇到了这个问题,而在开发机器上一切都正常。以下这些代码行帮助解决了问题:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.0" newVersion="1.5.2.14234"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

1
这已经至少帮了我两次了,因为当我再次想要给你点赞时,我意识到了这一点。你有什么想法,为什么错误版本只在我的生产服务器上被引用,就像你一样?正确的版本似乎是通过NuGet在VS中被引用的。谢谢 =) - frax
需要检查Fusion日志。可能是机器范围内的配置在开发PC上包含了重定向规则。 - Der_Meister
有趣的是,如果xmlns="urn:schemas-microsoft-com:asm.v1"也可以解决我的问题。不幸的是,我不再能够访问我提出问题时的代码了。 - Roman Mik
感谢您的回答。我刚刚在干净的WS 2008 / IIS 7.5上第一次部署MVC 4应用程序时遇到了同样的问题。我不知道这种行为是从哪里来的,因为我只是应用了修复程序,而没有进行融合日志/机器配置检查等操作,但它似乎与某些版本的.Net和其他组件有关,即使配置文件和GAC默认库都没有手动更改... - AFract
我不小心将WebGrease.dll文件从项目中排除,遇到了同样的问题,后来通过将其包含到项目中解决了。 - Sameer

17

最后,问题出在 <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 中。这导致了Render方法加载错误的WebGrease程序集。

移除xmlns对我来说解决了这个问题。


7
您的解决方案非常好。有人可以告诉我为什么吗? - Fried
我很想知道为什么这会破坏我的项目,因为这行代码需要在我的 web.config 的顶层,才能让我的 web.testing.config 实际替换连接字符串。删除它可以修复我的项目,但同时也停止了我的转换。 - blapsley
我阅读了各种回答,尝试了其中的几个,但都没有成功,直到我采用了这个答案中所示的方法,删除了“xmlns”引用才行。 - qxotk

6

我修改了我的web.config文件,使得newVersion="1.0.0.0"与我引用的文件版本相匹配:

<dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.0.0.0" />
  </dependentAssembly>

我已经尝试使用您的方法进行测试,但不幸的是它对我没有起作用。在我的情况下,是 xmlns 导致了问题。 - Roman Mik
我也试过了,只需确保将newVersion更改为与您的DLL匹配即可。在我的情况下,它是<bindingRedirect oldVersion="1.0.0.0-1.6.0.0" newVersion="1.3.0.0" />。 - Sameer Alibhai
我不得不移除整个"dependantAssembly"块才能解决这个问题。 - Flapper
我已经有一段时间没有实现这个解决方案了,所以我不记得具体是哪些设置负责,但我相信 newVersion="...." 需要与包版本匹配,oldVersion="..." 需要在其范围内包含新版本... 但必须兼容......? - DaniDev

5

如果有人需要帮助,我也遇到了同样的问题,但是发现是WebGrease的一个依赖程序集导致的,即Antlr3。当通过NuGet安装时,它会在web.configruntime元素中添加以下内容:

  <dependentAssembly>
    <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
  </dependentAssembly>

仅仅移除这一部分就解决了我的问题。


2
在我的情况下,问题最终被证明是我 web.config 文件中的一个 XML 处理指令 (PI) (<?blah ... ?>)。这是完全合法的 XML!但它导致了这个错误消息的出现,并让我在所有错误的地方寻找解决方法。
我的 web.config 看起来类似于以下内容 - 请注意 connectionStrings 部分中的 XML PI:
<configuration>
    ...
    <connectionStrings>
        <?blah ... ?>
        <add name="AppDb" ... />
    ...
    </connectionStrings>
    ...
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            ...
            <dependentAssembly>
                <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
            </dependentAssembly>
            ...
        </assemblyBinding>
    </runtime>
    ...
</configuration>

请注意,XML PI <?blah ... ?>出现在connectionStrings部分 - 也就是说,它与assemblyBinding部分或WebGrease等的bindingRedirect条目(这些是正确的!)没有关系。

1

我在使用 .net 4.5 的 web 表单网站时遇到了同样的问题。简单地更新 NuGet 包到最新版本帮助了我解决了这个问题。


1

我最终发现有两个针对WebGrease的运行时程序集绑定条目。删除旧的那个(版本为1.5.2)解决了我的问题。

<dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31BF3856AD364E35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>

<dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>

0
我曾经遇到过同样的问题,但这是因为我将解决方案从我的本地开发计算机复制到了一个网络驱动器上,我们在那里存储我们的项目。当我从映射的驱动器打开解决方案时,我无法使引用正常工作,一直收到这个错误。我找到的唯一临时解决方法是从其UNC路径而不是映射的驱动器字母打开解决方案。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接