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

Rom*_*Mik 17 c# asp.net asp.net-mvc bundling-and-minification asp.net-optimization

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

我试图将System.Web.Optimization添加到我的ASP.NET Web窗体解决方案.我通过NuGet包添加了Microsoft ASP.NET Web Optimization Framework.它将Microsoft.Web.Infrastracture和WebGrease(1.5.2)添加到引用中.

但是,当我跑

<%= System.Web.Optimization.Scripts.Render("~/bundles/js")%>
Run Code Online (Sandbox Code Playgroud)

我得到运行时错误

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)
Run Code Online (Sandbox Code Playgroud)

我已经尝试将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>
Run Code Online (Sandbox Code Playgroud)

但没有任何运气.

我注意到我的WebSite的Web配置包含这一行

 <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
Run Code Online (Sandbox Code Playgroud)

如果我用它替换它

 <configuration>
Run Code Online (Sandbox Code Playgroud)

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

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

如果不改变,我还能尝试什么

 <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> ?
Run Code Online (Sandbox Code Playgroud)

感谢您提供任何帮助!

编辑: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
Run Code Online (Sandbox Code Playgroud)

2)确认,问题在于

<configuration  xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
Run Code Online (Sandbox Code Playgroud)

但是,我不明白为什么

Der*_*ter 25

我在prod服务器上遇到了这个问题,而在开发人员机器上一切正常.这些行帮助:

<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>
Run Code Online (Sandbox Code Playgroud)


Rom*_*Mik 16

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

删除xmlns解决了我的问题.

  • 您的解决方案非常好.有人可以告诉我为什么吗? (7认同)

Dan*_*Dev 5

我修改了我的web.config文件,以便newVersion ="1.0.0.0"与我的Referenced文件版本匹配:

<dependentAssembly>
    <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-1.3.0.0" newVersion="1.0.0.0" />
  </dependentAssembly>
Run Code Online (Sandbox Code Playgroud)


Dan*_*plo 5

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

  <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>
Run Code Online (Sandbox Code Playgroud)

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