locked
Web API System.net.http.formatting - JSON.NET - Builds well locally but fails on build server ",Version=4.5.0.0..'' RRS feed

  • Question

  • User1619020204 posted

    I recently did some work on one of the older APIs and had to write some basic unit tests for the controller.

    So, my controller test setup looks like this (followed by some simple tests)

    using Newtonsoft.Json; (version 7.0.0 and was already part of the project)

    [Setup] public void Init(){ usersController = new UsersController { Request = new HttpRequestMessage(), Configuration = new HttpConfiguration() }; XmlMediaTypeFormatter xmlFormatter = usersController.Configuration.Formatters.XmlFormatter; xmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml")); usersController.Configuration.Formatters.Clear(); usersController.Configuration.Formatters.Add(xmlFormatter); usersController.Request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, usersController.Configuration); }


    The API project already uses NewtonSoft version 7.0.0. I had to reference it in my Test class as I have some tests that need to deserialize some JSON.

    I have checked the version on `web.config` and `Myapiproject.csproj` as well, which looks like below and also app.config

    **Web.config**
    
    <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
    </dependentAssembly>
    
    **MyAPIProject.csproj**
    
    <Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
    <SpecificVersion>False</SpecificVersion>
    <HintPath>..\SharedProject\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>


    **App.config**
    <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
    </dependentAssembly>
    

    Everything works well when built locally, but when built on the dev server, it fails with the below error

    SetUp : System.IO.FileLoadException : Could not load file or assembly 'Newtonsoft.Json, Version=4.5.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)
    
    [nunit2] at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
    [nunit2] at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
    [nunit2] at System.Web.Http.HttpConfiguration.DefaultFormatters()
    [nunit2] at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes)

    I searched for all libraries that are depending on NewtonSoft and found that Microsoft.AspNet.WebApi.Client.4.0.20710.0.nuspec had dependency on

    <dependency id="Newtonsoft.Json" version="4.5.6" />

    I'm certain that version 4.5.6 package no longer exists in the packages folder as it was removed and replaced by 7.0.0

    I think `HttpConfiguration` or `MediaFormatter` has dependency on the older version of NewtonSoft and the build is failing because the version it's looking for is no longer available.

    Also, I don't think I am referencing older version of NewtonSoftJson in my test class as I did an F12 on it and it showed it was version 7.0.0, and I think it's a a bit of a red herring that the project is failing on build server after I referenced JSON.NET on my test class.

    How can I solve this without having to update NewtonSoftJson across the whole solution as that will have impacts on other projects? I don't think I can update the Web API package either.

    I am sure it's similar to the issue here http://www.tomasvoracek.com/2015/07/could-not-load-file-or-assembly-newtonsoft-json-version4-5-0-0-cultureneutral-publickeytoken30ad4fe6b2a6aeed-or-one-of-its-dependencies/

    Thanks

    Wednesday, September 21, 2016 8:36 PM

Answers

  • User36583972 posted

    Hi Ren314,

    SetUp : System.IO.FileLoadException : Could not load file or assembly 'Newtonsoft.Json, Version=4.5.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)

    You should make sure that your computer had the same version in all locations and that your projects were all pointing to the same reference path.

    So, Maybe the easiest way will be to try and reinstall the package.

    Go to TOOLS > NuGet Package Manager and Select Package Manager Console. Execute the following two commands:

    uninstall-package newtonsoft.json -force
    
    install-package newtonsoft.json -version "7.0.0"
    

    The -force option in the first command is required to force the uninstall. Dependencies with other assemblies prevent the uninstall without it.

    Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0

    http://stackoverflow.com/questions/32794225/could-not-load-file-or-assembly-newtonsoft-json-version-7-0-0-0

    Best Regards,

    Yohann Lu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 22, 2016 9:42 AM