繁体   English   中英

无法加载文件或程序集“WebGrease”的依赖项之一。 定位的程序集的清单定义与程序集引用不匹配

[英]Could not load file or assembly 'WebGrease' one of its dependencies. The located assembly's manifest definition does not match the assembly reference

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

我正在尝试将 System.Web.Optimization 添加到我的 ASP.NET Web 窗体解决方案中。 我通过 NuGet 包添加了 Microsoft ASP.NET Web 优化框架。 它在参考中添加了 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 配置包含这一行

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

如果我用它替换它

 <configuration>

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

当模式指向 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">

然而,我不明白为什么

我在生产服务器上遇到了这个问题,而在开发者机器上一切正常。 这些行有帮助:

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

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

删除 xmlns 为我解决了这个问题。

我修改了我的 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>

以防万一它对任何人有帮助,我遇到了同样的问题,但发现它是由 WebGrease 的依赖程序集引起的,即Antlr3 通过 NuGet 安装runtime它已将以下内容添加到web.config runtime元素:

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

只需删除它即可解决我的问题。

就我而言,问题出在我的 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条目WebGrease (这是正确的!)。

在 Web 表单站点 .net 4.5 中遇到同样的问题,将 nuget 包简单更新到最新版本对我有帮助。

我最终得到了 WebGrease 的 2 个运行时 assemblyBinding 条目。 删除旧的(版本 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>

我遇到了同样的问题,但这是将解决方案从我的本地开发计算机复制到我们存储项目的网络驱动器的结果。 当我从映射驱动器打开解决方案时,我无法使参考正常工作,并且我一直收到此错误。 我为我的特定问题找到的唯一临时解决方法是从其 UNC 路径而不是映射的驱动器号打开解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM