从 vs2013 升级到 vs2017 后无法加载文件或程序集 'System.Web.Http, Version=4.0.0.0'

Could not load file or assembly 'System.Web.Http, Version=4.0.0.0' after upgrading from vs2013 to vs2017

我们最近从 Visual Studio Community 2013 升级到 Community 2017。我的 REST 服务项目在启动后开始崩溃(构建通过)。这只发生在三分之一的机器上。 我们正在使用 Microsoft.AspNet.WebApi.Owin 创建我们的 REST 端点。

问题似乎与加载 System.Web.Http 有关,该库引用了 {solution_folder}/packages/Microsoft.AspNet.WebApi.Core.5.2.3/lib/net45/System.Web.Http.dll.

引用的dll在引用的文件夹中,重建时也会复制到bin文件夹中。有趣的是,当我用 VS2013 重建项目时,我可以从 vs2013 或 vs2017 手动启动服务,但是当我用 vs2017 重建项目并启动它时,我得到以下错误:

Inner Exception 1: 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)

堆栈跟踪:

System.Reflection.TargetInvocationException occurred
HResult=0x80131604 Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Owin.Loader.DefaultLoader.<>c__DisplayClass12.b__b(IAppBuilder builder) at Owin.Loader.DefaultLoader.<>c__DisplayClass1.b__0(IAppBuilder builder) at Microsoft.Owin.Hosting.Engine.HostingEngine.ResolveApp(StartContext context) at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context) at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions options) at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options) at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider services, StartOptions options) at Microsoft.Owin.Hosting.WebApp.Start(StartOptions options) at Microsoft.Owin.Hosting.WebApp.Start[TStartup](StartOptions options)
at Microsoft.Owin.Hosting.WebApp.Start[TStartup](String url) at PNX.REST.Service.ServiceAdapter.Initialize() in C:\Projects\peaknx\PNX.OS\PNX.REST.Service\ServiceAdapter.cs:line 32
at PNX.REST.Service.PNXRESTService.StartAdapter(Object state) in C:\Projects\peaknx\PNX.OS\PNX.REST.Service\PNXRESTService.cs:line 85
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()

Inner Exception 1: 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)

更新:

似乎只有在我的机器上构建才会将 app.config 文件复制为 ProjectName.Service.exe.config,而在所有其他机器上它使用 Web.config.

基本上我从来没有进行依赖重定向,它总是寻找 System.Web.Http 4.0.0.0 而不是 5.2.3.0,因为这部分配置不在 ProjectName.Service.exe.config.

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

已解决:问题是在安装 VS2017 时,您必须在工作负载中检查 "ASP.NET and web development",以便 VS 将项目识别为 Web 项目并使用 Web.config 而不是 app.config

不幸的是,如果您错过了这一步,将不会有任何有用的错误报告,希望这对某人有所帮助,感谢您的回答。