4

I seem to be having trouble loading System.Net.Http.Formatting.dll in a VS2010 ConsoleApplication, even though I have redirects set up. Here's the error

System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   at uk.ac.salford.accman.backend.service.Models.WebService.AlmaRestClient.CreateOrUpdateUser(Operator user, Account account, JanusUser janusUser)
   at Salford.AccMan.BackEnd.Processes.Janus.UpdateALMAUser.ProcessTransaction(Transaction& transaction) in C:\VS2010\Accman.NET\AccManBackEndLibrary\Processes\Janus\UpdateALMAUser.cs:line 121
   at Salford.AccMan.BackEnd.Processes.TransactionServiceProcess.Process() in C:\VS2010\Accman.NET\AccManBackEndLibrary\TransactionServiceProcess.cs:line 147

=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : AccManLibrary, Version=3.0.4.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in LoadFrom load context.
WRN: Native image will not be probed in LoadFrom context. Native image will only be probed in default load context, like with Assembly.Load().
LOG: Using application configuration file: C:\VS2010\.NET Code Library\ConsoleSchedulerService\ConsoleSchedulerService\bin\Debug\ConsoleSchedulerService.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.0.0.0 redirected to 4.0.21112.0.
LOG: Post-policy reference: System.Net.Http.Formatting, Version=4.0.21112.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/redirect/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/redirect/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/System.Net.Http.Formatting/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/redirect/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/.NET Code Library/ConsoleSchedulerService/ConsoleSchedulerService/bin/Debug/redirect/System.Net.Http.Formatting/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/Accman.NET/AccManBackEndLibrary/bin/System.Net.Http.Formatting.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Build Number
LOG: Attempting download of new URL file:///C:/VS2010/Accman.NET/AccManBackEndLibrary/bin/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/VS2010/Accman.NET/AccManBackEndLibrary/bin/System.Net.Http.Formatting.EXE.
LOG: Attempting download of new URL file:///C:/VS2010/Accman.NET/AccManBackEndLibrary/bin/System.Net.Http.Formatting/System.Net.Http.Formatting.EXE.

So, as I understand it, it's looking for System.Net.Http.Formatting, Version=4.0.0.0, but my project contains version 4.0.21112.0, so I set up a redirect (which is also mentioned in the stack trace so I know the redirect is at least being picked up)

My redirect is:

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

It finds the newer DLL in C:/VS2010/Accman.NET/AccManBackEndLibrary/bin, but complains the build number isn't right. However, as far as I can tell, the entire version number is correct.

If I run a Powershell command to get the exact version number it returns the same number:

PS F:\> (get-item C:\VS2010\Accman.NET\AccManBackEndLibrary\bin\System.Net.Http.Formatting.dll).VersionInfo

ProductVersion   FileVersion      FileName
--------------   -----------      --------
4.0.21112.0      4.0.21112.0      C:\VS2010\Accman.NET\AccManBackEndLibrary\bin\System.Net.Http.Formatting.dll

Please help! What am I doing wrong?

Thanks

Richard
  • 1,731
  • 2
  • 23
  • 54

3 Answers3

2

You could remove the dll reference from your project and manually re-add the dll you have.
(Solution Explorer -> References -> Add -> Browse)
Should fix the problem.

user5226582
  • 1,946
  • 1
  • 22
  • 37
  • Thanks, do you mean remove the reference from the project in Visual Studio? – Richard Oct 14 '15 at 15:15
  • Sorry for the delay in replying. This doesn't seem to have fixed the problem unfortunately but I'm going to remove everything and double-check there are no lingering references anywhere at all before trying to re-add them (manually) rather than using Nuget and see if I can get it working. Thanks – Richard Oct 15 '15 at 08:15
  • Nope, same problem. I even removed the references from packages.config before manually adding the references back in. What I don't get is that it confirms that its looking for (via a redirect) version 4.0.21112.0, and then you can see where it finds the DLL, in the right place, with that exact same version. But it still says the build numbers don't match... and it's not like there are multiple versions of that DLL in that folder. I'm very confused... – Richard Oct 15 '15 at 11:07
  • Fixed it. I actually have two possible answers which I'm about to post in case they help someone else, but I've marked yours as useful because it helped me get further so many thanks for your help on this :) – Richard Oct 15 '15 at 13:45
2

Answer 1

Right, first answer if you want to use the version of System.Net.Http.Formatting, System.Net.Http and System.Net.WebRequest that comes bundled with .NET 4, which is what my (legacy) project uses.

  • Remove every single reference to these packages (including packages.config and in Nuget package manager). Also if your problem came about because you installed NewtonSoft.Json from Nuget like me, remove that too.

  • Manually add them as @user5226582 suggested (right click on References in the project -> Add Reference -> Assemblies -> Extensions. Note I chose them from the bundled Extensions Assemblies.

  • Also, do the same for NewtonSoft.Json.dll if you're using it. That is also in Assemblies -> Extensions.

  • Remove any bindingRedirects relating to these files from app.config

Now it's a level playing field because all DLLs have come from the same place, and should be compatible with one another.

Answer 2

System.Net.Http has been replaced by Microsoft.Net.Http. Remove any existing references and related bindingRedirects (as in answer 1).

  • Use Nuget to install Microsoft.Net.Http
  • Use Nuget to install NewtonSoft.Json (if using)

That seems to work for me

Richard
  • 1,731
  • 2
  • 23
  • 54
0

The package file version and assembly version does not necessarily line up:

https://stackoverflow.com/a/51736117/3383751

maxpaj
  • 6,029
  • 5
  • 35
  • 56