FileLoadException with Newtonsoft.Json trying to use MapHttpRoute in 4.5.1 WebForms

495 views Asked by At

I added a 2.1 Web API Controller Class to my 4.5.1 WebForms application. Afterwards, my packages.config looked like:

  <package id="Microsoft.AspNet.WebApi" version="5.2.7" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.7" targetFramework="net451" />
  <package id="Newtonsoft.Json" version="12.0.2" targetFramework="net451" />

My Web.config looks like this:

<runtime>
    <assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
        </dependentAssembly>
    </assemblyBinding>
</runtime>

I was following the instructions from this tutorial: https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/using-web-api-with-aspnet-web-forms

I added the following to Application_Start:

RouteTable.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

However, when I run and it attempts to run this line, I get the following error:

System.IO.FileLoadException
  HResult=0x80131040
  Message=Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
  Source=System.Net.Http.Formatting
  StackTrace:
   at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter..ctor()
   at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
   at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
   at System.Web.Http.HttpConfiguration.DefaultFormatters(HttpConfiguration config)
   at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes)
   at System.Web.Http.GlobalConfiguration.<>c.<CreateConfiguration>b__11_0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at System.Web.Http.GlobalConfiguration.get_Configuration()
   at System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults, Object constraints, HttpMessageHandler handler)
   at System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults)
   at Modeler.Global.Application_Start(Object sender, EventArgs e) in C:\BTR\Evolution\Websites\ESS\DRA\Global.asax.cs:line 13

I've tried removing the binding redirect info in web.config and received the same error.

Update

Originally, my library dlls referenced 11.0.2 of Newtonsoft. I updated them to 12.0.2 in an attempt to make things work which didn't. So I put all my projects (including the web site) back to 11.0.2 and still have the same problem. But I wanted to note, that I tested in a Webforms project without referencing my library dlls which already had a 12.0.3 reference to Newtonsoft and it worked. So there is something going on with the fact that I have dependent dlls that reference Newtonsoft, but I don't know what it could be.

All reference 11.0.2 and when the site is running (if I comment out call to MapHttpRoute), AppDomain.CurrentDomain.GetAssemblies() correctly shows:

{Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed}

Fusion Log

*** Assembly Binder Log Entry  (4/16/2021 @ 10:44:07 AM) ***

The operation failed.
Bind result: hr = 0x80131040. No description available.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\Program Files (x86)\IIS Express\iisexpress.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = file:///C:/BTR/Evolution/Websites/ESS/DRA/
LOG: Initial PrivatePath = C:\BTR\Evolution\Websites\ESS\DRA\bin
LOG: Dynamic Base = C:\Users\terry.aney\AppData\Local\Temp\Temporary ASP.NET Files\vs\66fd6d3a
LOG: Cache Base = C:\Users\terry.aney\AppData\Local\Temp\Temporary ASP.NET Files\vs\66fd6d3a
LOG: AppName = 92fcbfc0
Calling assembly : System.Net.Http.Formatting, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\BTR\Evolution\Websites\ESS\DRA\web.config
LOG: Using host configuration file: C:\Users\terry.aney\OneDrive\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: Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/terry.aney/AppData/Local/Temp/Temporary ASP.NET Files/vs/66fd6d3a/92fcbfc0/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/terry.aney/AppData/Local/Temp/Temporary ASP.NET Files/vs/66fd6d3a/92fcbfc0/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/BTR/Evolution/Websites/ESS/DRA/bin/Newtonsoft.Json.DLL.
LOG: Assembly download was successful. Attempting setup of file: C:\BTR\Evolution\Websites\ESS\DRA\bin\Newtonsoft.Json.dll
LOG: Entering download cache setup phase.
LOG: Assembly Name is: Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: The assembly reference did not match the assembly definition found.
ERR: Setup failed with hr = 0x80131040.
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
0

There are 0 answers