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

0

we recently upgraded from Visual Studio Community 2013 to Community 2017. My REST Service project started breaking after starting it (build passes). This is only happening on one out of 3 machines. We are using Microsoft.AspNet.WebApi.Owin for creating our REST endpoints.

The issue seems to be on loading System.Web.Http, the library is referenced to {solution_folder}/packages/Microsoft.AspNet.WebApi.Core.5.2.3/lib/net45/System.Web.Http.dll.

The referenced dll is in referenced folder and also copies to bin folder when rebuilding. Interesting thing is that when I rebuild the project with VS2013 I can start the service manually, from vs2013 or vs2017, but when I rebuild the project with vs2017 and start it I get the following error:

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)

Stacktrace:

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)

Update:

It seems that only on my machine the build copies app.config file as ProjectName.Service.exe.config, where as on all others it uses Web.config.

Basically I never get my dependency redirection going and it always looks for System.Web.Http 4.0.0.0 instead of 5.2.3.0 since this part of configuration is not in 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>
c#
asp.net
.net
asp.net-web-api
owin
asked on Stack Overflow Oct 3, 2017 by ivke • edited Oct 3, 2017 by ivke

1 Answer

0

SOLVED: The problem was that when installing VS2017 you must check "ASP.NET and web development" in workloads in order for VS to recognize the project as Web project and use Web.config instead of app.config

Unfortunately if you miss this step there will be no useful error report, hope this helps someone thanks for the answers.

answered on Stack Overflow Oct 3, 2017 by ivke

User contributions licensed under CC BY-SA 3.0