11

I'm using AspNet Web Api Client 5.0 and i am trying to unit test a web api controller.

var encservice = new EncryptionService();
var acctservice = FakeServices.GetAccountService();
var controller = new AccountController(acctservice, encservice);
controller.Request = new HttpRequestMessage();

when the code

controller.Request.SetConfiguration(new HttpConfiguration());

is executed i hit an exception

Message: 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)

Source: System.Net.Http.Formatting

Stacktrace: at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters() at System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor() at System.Web.Http.HttpConfiguration.DefaultFormatters() at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes) at System.Web.Http.HttpConfiguration..ctor() at EMR.Test.Controller.AccountControllerTest.Should_Get() in c:\PremiumProjectsCollection\emr\src\EMRAzure\EMRAzure\EMR.Test\Controller\AccountControllerTest.cs:line 34

the version of newsoft.json that i am using is 6.0

I also have a assembly redirection in my confguration file

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

The test runner that im using is MStest, VS2012

4
  • I am facing the same issue. Did you ever resolve it? Commented Mar 6, 2014 at 21:53
  • @Jean-FrançoisBeauchamp, I ended up rolling back to Newtonsoft.Json 4.5.0.0 Commented Mar 7, 2014 at 8:11
  • I'm having the same issue. Did you ever get this issue resolved? Commented Apr 8, 2014 at 17:24
  • @ChuckConway no I ended up rolling back to Newtonsoft.Json 4.5.0.0 Commented Apr 10, 2014 at 5:52

4 Answers 4

5

You'll need to add an assembly redirect:

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

(assuming that the assembly version of Newtonsoft.Json is exactly 6.0.0.0.)

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks mark, but i already have this in my app.config. and it still hits exception.
Which app.config file?
yes, i have it in my app.config. in the unit test project
I am using mstest + vs2012
IME, the exception message you've posted is almost always caused by differing assembly versions. The fix is always an assembly redirect. However, that only works if the publicKeyToken remains constant across versions. Have you checked whether or not that's the case? IIRC, there may have been some issues with Newtonsoft.Json changing public key token, but I may misremember...
3

(note comment below is referring to a Web Api Client project having this issue)

I had the same problem with the version of Newtonsoft.Json so I deleted the older version references and used Package Manager Console to install the latest version of Newtonsoft.Json on my Web Api Client Library and Test Project.

Install-Package Newtonsoft.Json -Version 6.0.8 (note u might need to find out which is the latest version)

The problem remained so I realized there is a crash between System.Net.Http.Formatting and my latest version of Json. To solve this, delete the System.Net.Http and System.Net.Http.Formatting references and install the WebApi Client Library via Nuget instead, as below:

Install-Package Microsoft.AspNet.WebApi.Client

This solved it for me.

Comments

1

I did not try this myself, but there seems to be a bug in 2012 mstest. Where you have to use .testsettings file for app.config to be picked up.

See the following link: http://social.msdn.microsoft.com/Forums/vstudio/en-US/234926d1-42c0-4ebb-af39-1626e72f6c39/why-does-assemblybinding-work-only-if-testsettings-file-is-used-vs2012rc?forum=vsunittest

Comments

0

I faced the same problem days ago and it took me hours to find a solution for this one.

I was testing a unit that had the latest version of NewtonSoft installed while my test project had an older version.

What i did to get around this was consolidate the versions of this library in my solution via the "Manage Nuget packages for solution" option by right clicking the solution in solution explorer.

This will update all NewtonSoft libraries present in your projects under the current solution and remove all old versions from the package control VisualStudio creates in a folder named packages in your solution directory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.