title: Could not load file or assembly 'System.Web.Http, Version=4.0.0.0' after upgrading from vs2013 to vs2017
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <blockquote>
    <p>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)</p>
  </blockquote>
  
  <p>Stacktrace:</p>
  
  <blockquote>
    <p>System.Reflection.TargetInvocationException occurred<br>
    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)<br>
    at
    Owin.Loader.DefaultLoader.&lt;>c__DisplayClass12.b__b(IAppBuilder
    builder)    at
    Owin.Loader.DefaultLoader.&lt;>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)<br>
    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<br>
    at PNX.REST.Service.PNXRESTService.StartAdapter(Object state) in
    C:\Projects\peaknx\PNX.OS\PNX.REST.Service\PNXRESTService.cs:line 85<br>
    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()</p>
    
    <p>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)</p>
  </blockquote>
  
  <p>Update:</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>&lt;dependentAssembly&gt; &lt;assemblyIdentity name="System.Web.Http"
  publicKeyToken="31bf3856ad364e35" culture="neutral" /&gt;
  &lt;bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /&gt;
  &lt;/dependentAssembly&gt;
  </code></pre>