C# 从 2012 年到 2013 年更新后无法加载文件或程序集“System.Web.Http 4.0.0”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19491860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:08:04  来源:igfitidea点击:

Could not load file or assembly 'System.Web.Http 4.0.0 after update from 2012 to 2013

c#asp.net-web-apiodatavisual-studio-2013

提问by Michal Asaf Carmeli

I did the upgrade according to. http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and-web-api-2

我做了升级根据。 http://www.asp.net/mvc/tutorials/mvc-5/how-to-upgrade-an-aspnet-mvc-4-and-web-api-project-to-aspnet-mvc-5-and- web-api-2

I get the error. Does any one else got this error?

我得到了错误。有没有其他人得到这个错误?

Server Error in '/' Application.

Could not load file or assembly 'System.Web.Http, Version=4.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)
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileLoadException: Could not load file or assembly 'System.Web.Http, Version=4.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)

Source Error:

Line 48: Line 49: Line 50: } Line 51: Line 52: protected void Application_BeginRequest(object sender, EventArgs e)

Source File: d:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\Global.asax.cs Line: 50

Assembly Load Trace: The following information can be helpful to determine why the assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' could not be loaded.

=== Pre-bind state information === LOG: DisplayName = System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (Fully-specified) LOG: Appbase = file:///D:/SM/7.4Test/WebAPIServerV2/ThirdPartyWebAPI/ LOG: Initial PrivatePath = D:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\bin Calling assembly : ThirdPartyWebAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null. === LOG: This bind starts in default load context. LOG: Using application configuration file: D:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\web.config LOG: Using host configuration file: C:\Users\michalc\Documents\IISExpress\config\aspnet.config LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/9184b2ea/c6d4b139/System.Web.Http.DLL. LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/9184b2ea/c6d4b139/System.Web.Http/System.Web.Http.DLL. LOG: Attempting download of new URL file:///D:/SM/7.4Test/WebAPIServerV2/ThirdPartyWebAPI/bin/System.Web.Http.DLL. WRN: Comparing the assembly name resulted in the mismatch: Major Version ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Stack Trace:

[FileLoadException: Could not load file or assembly 'System.Web.Http, Version=4.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)] ThirdPartyWebAPI.WebApiApplication.Application_Start() in d:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\Global.asax.cs:50

[HttpException (0x80004005): Could not load file or assembly 'System.Web.Http, Version=4.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)]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9935033
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Could not load file or assembly 'System.Web.Http, Version=4.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)]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9913572 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

“/”应用程序中的服务器错误。

无法加载文件或程序集“System.Web.Http,版本=4.0.0.0,文化=中性,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)
说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.IO.FileLoadException:无法加载文件或程序集“System.Web.Http,版本=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)

源错误:

第 48 行:第 49 行:第 50 行:} 第 51 行:第 52 行:protected void Application_BeginRequest(object sender, EventArgs e)

源文件:d:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\Global.asax.cs 行:50

程序集加载跟踪:以下信息有助于确定无法加载程序集“System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”的原因。

=== 预绑定状态信息 === LOG: DisplayName = System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (Fully-specified) LOG: Appbase = file:///D: /SM/7.4Test/WebAPIServerV2/ThirdPartyWebAPI/ 日志:初始 PrivatePath = D:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\bin 调用程序集:ThirdPartyWebAPI,版本=1.0.0.0,Culture=neutral,PublicKeyToken=null。=== LOG:此绑定在默认加载上下文中启动。LOG:使用应用程序配置文件:D:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\web.config LOG:使用主机配置文件:C:\Users\michalc\Documents\IISExpress\config\aspnet.config LOG:使用机器配置文件来自 C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config。LOG:后政策参考:System.Web.Http,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35 日志:正在尝试下载新的 URL 文件:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/9184b2ea/c6d4b139/System.Web.Http.DLL。日志:正在尝试下载新的 URL 文件:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/9184b2ea/c6d4b139/System.Web.Http/System.Web.Http .DLL。日志:正在尝试下载新的 URL 文件:///D:/SM/7.4Test/WebAPIServerV2/ThirdPartyWebAPI/bin/System.Web.Http.DLL。警告:比较程序集名称导致不匹配:主要版本 ERR:无法完成程序集设置 (hr = 0x80131040)。探测终止。正在尝试下载新的 URL 文件:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/9184b2ea/c6d4b139/System.Web.Http/System.Web.Http.DLL . 日志:正在尝试下载新的 URL 文件:///D:/SM/7.4Test/WebAPIServerV2/ThirdPartyWebAPI/bin/System.Web.Http.DLL。警告:比较程序集名称导致不匹配:主要版本 ERR:无法完成程序集设置 (hr = 0x80131040)。探测终止。正在尝试下载新的 URL 文件:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/9184b2ea/c6d4b139/System.Web.Http/System.Web.Http.DLL . 日志:正在尝试下载新的 URL 文件:///D:/SM/7.4Test/WebAPIServerV2/ThirdPartyWebAPI/bin/System.Web.Http.DLL。警告:比较程序集名称导致不匹配:主要版本 ERR:无法完成程序集设置 (hr = 0x80131040)。探测终止。未能完成程序集的设置 (hr = 0x80131040)。探测终止。未能完成程序集的设置 (hr = 0x80131040)。探测终止。

堆栈跟踪:

[FileLoadException: 无法加载文件或程序集“System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)] ThirdPartyWebAPI.WebApiApplication.Application_Start() in d:\SM\7.4Test\WebAPIServerV2\ThirdPartyWebAPI\Global.asax.cs:50

[HttpException (0x80004005): 无法加载文件或程序集“System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(从HRESULT异常:0x80131040)]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext的上下文中,一个HttpApplication应用程序)9935033
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr的appContext,HttpContext的上下文中,MethodInfo的[]处理程序)118
System.Web.HttpApplication .InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): 无法加载文件或程序集“System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9913572 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254



Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408

版本信息:Microsoft .NET Framework 版本:4.0.30319;ASP.NET 版本:4.0.30319.18408

回答by Aliostad

You need to add assembly redirects:

您需要添加程序集重定向:

<configuration>

   ....

   <runtime>
      <assemblyBinding>
    <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      </assemblyBinding>
   </runtime>

   ...

</configuration>

Most likely you have to do this for a few more assemblies like webhosting, etc.

很可能您必须为更多程序集(如虚拟主机等)执行此操作。

回答by Sameer Alibhai

Or you could do this from NuGet Package Manager Console

或者您可以从 NuGet 包管理器控制台执行此操作

 Install-Package Microsoft.AspNet.WebApi -Version 5.0.0

And then you will be able to add the reference to System.Web.Http.WebHost 5.0

然后您将能够添加对 System.Web.Http.WebHost 5.0 的引用

回答by Jim Taliadoros

Just an obvious but possible helpful hint....remember to check that the new version you specify in your webconfig assembly binding is the same version that you reference in your project references. (ie as I write this...this would be 5.1.0.0 if you have recently done a NUGet on System.Web.Http

只是一个明显但可能有用的提示....请记住检查您在 webconfig 程序集绑定中指定的新版本是否与您在项目引用中引用的版本相同。(即在我写这篇文章时...如果您最近在 System.Web.Http 上完成了 NUGet,这将是 5.1.0.0

回答by Pathoschild

I fixed this by reinstalling the NuGet package, which corrects broken dependencies. From the package manager, run:

我通过重新安装 NuGet 包解决了这个问题,它更正了损坏的依赖项。从包管理器中,运行:

Update-Package Microsoft.AspNet.WebApi -reinstall

回答by Catto

To resolve the Could not load file or assembly 'System.Web.Http' error use NuGet to install the Web API 2.1 WebHost.

要解决无法加载文件或程序集“System.Web.Http”错误,请使用 NuGet 安装 Web API 2.1 WebHost。

In solution explorer in the references right click and select manage nuget packages. (if not there install nuget)

在解决方案资源管理器中,右键单击并选择管理 nuget 包。(如果没有安装nuget)

enter image description here

在此处输入图片说明

In the manage NuGet Packages window on the left side click online then in top right search for Web Host and install Microsoft ASP.NET Web API 2.1 Web Host. (Once installed the install button will change to a green check)

在左侧的管理 NuGet 包窗口中单击在线,然后在右上角搜索 Web Host 并安装 Microsoft ASP.NET Web API 2.1 Web Host。(安装后,安装按钮将变为绿色对勾)

After that the project will reload and when it's build again the error will be resolved and the project will debug and run. The error will be gone and you may be 'The resource cannot be found. Just append the url ( ex from localhost:52088/ to localhost:52088api/products )

之后项目将重新加载,当它再次构建时,错误将得到解决,项目将调试和运行。错误将消失,您可能是“找不到资源。只需附加网址(例如从 localhost:52088/ 到 localhost:52088api/products )

Your question was good and helped me.

你的问题很好,对我有帮助。

Hope this answer helps!

希望这个回答有帮助!

回答by jhilden

When I installed Web Api 2.2 help this error started. I added this to the web.config and it solved my problem.

当我安装 Web Api 2.2 时,这个错误开始了。我将此添加到 web.config 并解决了我的问题。

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

回答by DeeArgee

As others have said just reinstall the MVC package to your web project using nuget, but be sure to add the MVC package to any projects depending on the web project, such as unit tests. If you build each included project individually, you will see witch ones require the update.

正如其他人所说,只需使用 nuget 将 MVC 包重新安装到您的 web 项目中,但一定要根据 web 项目将 MVC 包添加到任何项目,例如单元测试。如果单独构建每个包含的项目,您将看到女巫需要更新。

回答by Mohammadreza

I had same problem and reinstalling package doesn't work.

我有同样的问题,重新安装包不起作用。

My application runs in LocalIIS(not IIS EXPRESS). I enable Assembly Load Trace and find out it want load System.Web.Httpload from below temp catche. so remove all catch foldersin it.

我的应用程序在 LocalIIS(不是 IIS EXPRESS)中运行。我启用了Assembly Load Trace 并发现它想要从temp catche 下面加载System.Web.Http负载。所以删除其中的所有catch文件夹

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files[my application]

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files[我的应用程序]

回答by ravula sandeep

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

回答by Ashitosh birajdar

I found the solution ! Delete folder named package in project directory and then rebuild to run it.

我找到了解决方案!删除项目目录中名为 package 的文件夹,然后重新构建以运行它。