【问题标题】:Cannot load file after installing CORS安装 CORS 后无法加载文件
【发布时间】:2014-03-20 21:13:22
【问题描述】:

我使用所有默认值在 VS Express 2013 中创建了一个基本的 ASP.NET Web Api 应用程序。我添加了一个控制器,它会按照我的意愿返回 XML。

只要我安装了 CORS 包:

Install-Package Microsoft.AspNet.WebApi.Cors -Pre

我什至不能再运行应用程序了:

An exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary

Additional information: Could not load file or assembly 'System.Web.Http, Version=5.0.0.0, 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)

我遵循了这篇博文中的所有说明:http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api,其中没有说明这个问题。

我尝试使用以下方式更新 Web API:

Install-Package Microsoft.AspNet.WebApi -Pre

但不幸的是问题仍然存在

在项目中,我使用的是 WebApi.Hal,它依赖于 Web.Api 5.0 及更高版本。是否有任何重大变化?

重现问题的绝对最小值

  1. 创建新的、完全空的 Web API 项目 n VS Express for Web 2013
  2. 添加一个控制器并测试它的工作原理
  3. 安装 CORS
  4. 文件加载错误 - 控制器不再工作
  5. 升级 Web Api - 修复问题

    安装包 Microsoft.AspNet.WebApi -Pre

  6. 安装 WebApi.Hal - 再次出现同样的错误

我可以使用不会杀死其他所有东西的旧版本 CORS 库吗?

详细的堆栈跟踪

=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:
LOG: Initial PrivatePath = C
Calling assembly : WebApi.Hal, Version=2.2.0.18, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:
LOG: Using host configuration file: C:\
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C: Files/root/aa0e7960/9dfdb45e/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C: Files/root/aa0e7960/9dfdb45e/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/CORSTest/CORSTest/bin/System.Net.Http.Formatting.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

【问题讨论】:

  • 在 VS2015 中 - System.Web.Cors dll 仍然存在此问题,并且已尝试上述所有方法...当前版本的软件包是 6.0.1 预览版。但由于与帮助页面 dll 冲突,无法升级到该版本

标签: c# asp.net .net asp.net-web-api


【解决方案1】:

两步解决问题

  1. 添加

    依赖程序集 assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" 依赖程序集

到 web.config

  1. Go tool/Library Packed Manager/Manage Nuget Package For solution,然后单击 Microsoft ASP.Net MVC,然后单击更新。

希望对你有帮助

谭阮

【讨论】:

    【解决方案2】:

    问题是由于 System.Net.Http.Formatting 以及 System.Web.Http 程序集的版本冲突造成的。如果您在构建代码后看到输出窗口,它会为您提供问题的解决方案。您所要做的就是将以下内容添加到您的 Web.config 文件中:

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

    【讨论】:

      【解决方案3】:

      尝试重新安装:

      Install-Package Microsoft.AspNet.WebApi.Cors
      

      【讨论】:

        【解决方案4】:

        Microsoft.AspNet.WebApi.Cors 包引入了对 5.2.3.0 版 dll 的依赖项,如用户 sumit 所示。我采取的路径是更新 MVC 项目 proir 以安装 CORS:

        Install-Package Microsoft.AspNet.Mvc -Version 5.2.3
        

        【讨论】:

          【解决方案5】:

          有两个步骤可以解决这个问题

          1. 更新asp.net mvc nuget包

            安装包 Microsoft.AspNet.Mvc -Version 5.2.3

          2. 使用新的dependentAssembly 更新 web.config

            assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.2.3.0"

          【讨论】:

            猜你喜欢
            • 2014-10-13
            • 1970-01-01
            • 1970-01-01
            • 2018-12-26
            • 1970-01-01
            • 1970-01-01
            • 2013-10-31
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多