Error unit testing webapi controller - c#

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

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.)

(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.

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

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.

Related

Cannot build clean VSIX project: Tries to load Newtonsoft.Json which is not there?

The following build error is pestering me.
The "CompareCommonBuildTaskVersion" task could not be loaded from the assembly C:\Users\Thijs\.nuget\packages\microsoft.vssdk.buildtools\16.6.1026\tools\VSSDK\Microsoft.VisualStudio.Sdk.BuildTasks.dll.
Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
Confirm that the <UsingTask> declaration is correct, that the assembly and all its dependencies are available, and that the task contains a public class that implements Microsoft.Build.Framework.ITask.
I just checked the directory mentioned, and there is a Newtonsoft.Json.dll there, but it's version 12.0.2.23222 (File Version).
I'm using:
Visual Studio Enterprise 2019 Preview
Version 16.6.0 Preview 1.0
Visual Studio Enterprise 2019
Version 16.5.0
Upon reading the link EricEJ posted I checked my devenv.exe.config which has the following (correct) AssemblyRedirect:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="4.5.0.0-12.0.0.0" newVersion="12.0.0.2"/>
<codeBase version="12.0.0.2" href="PrivateAssemblies\Newtonsoft.Json.12.0.0.2\Newtonsoft.Json.dll"/>
</dependentAssembly>
Problem seems to be with the actual BuildTools package (see answer).
I tried the different BuildTools package versions again, starting from the newest and after installing, restarted VS and then did a rebuild of the solution:
Version 16.6.1026 -> March -> Not working
Version 16.6.1024 -> March -> Not working
Version 16.5.2044 - February -> Working
So far it seems the downgrade has resolved my issues.

Could not load file or assembly 'System.Spatial' or one of its dependencies

I get this error message:
Could not load file or assembly 'System.Spatial, Version=5.7.0.0,
Culture=neutral, PublicKeyToken=49ba329had364evz' or one of its
dependencies. The located assembly's manifest definition does not
match the assembly reference. (Exception from HRESULT: 0x80131040)
I tried to uninstall/update System.Spatial and WindowsAzure.Storage Nuget packages but still get the error.
It's a dependency of WindowsAzure.Storage package, but funny thing is that I don't get this error when running my ASP.NET Web API project locally, but only on the Azure server.
I don't know where to find the assembly's manifest, is it packages.config?
How to get rid of this error? I already wasted two days :(
Changed the version details in configuration file based on DLL version present in bin folder of project having issue.
In configuration file it was referring to version 5.6.4 but I have changed to version 5.6.3 which resolved the error.
Before Change
<dependentAssembly>
<assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" />
</dependentAssembly>
After change.
<dependentAssembly>
<assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.6.3.0" newVersion="5.6.3.0" />
</dependentAssembly>
if you are using visual studio, go to nugget package manager and view installed packages, and use update pane to update system.spatial package,
this solved my issue
For some reason I don't know, Visual Studio wasn't uploading newer .dll file to the server.
So I manually uploaded System.Spatial version 5.7.0 on the server.
(although still I don't know how could I change the version in a so called manifest.)
Fit all dependencies to your current azure version by the following two steps:
open Package Manager Console window
enter PM> Install-Package WindowsAzure.Storage -Version [your_version_number]-preview -Pre
this is what solved the problem in my case.

Google.Apis.Calendar.v3.dll ask 1.9.2 version on Google APIs

I setup nuget packages
Install-Package Google.Apis.Core
Install-Package Google.Apis
Install-Package Google.Apis.Auth
As it said there http://google-api-dotnet-client.blogspot.ca/
We have released a 1.9.3 version of NuGet packages only for the Core packages - that’s where the new features are. You should still use 1.9.2 version of the API specific packages (such as Drive) and only update the Core packages
Install-Package Google.Apis.Calendar.v3
When I tried to build it I'm getting error:
Assuming assembly reference 'Google.Apis, Version=1.9.2.27817, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab' matches 'Google.Apis, Version=1.9.3.19379, Culture=neutral, PublicKeyToken=4b01fa6e34db77ab',
you may need to supply runtime policy
I added redirect script in web.config:
<dependentAssembly>
<assemblyIdentity name="Google.Apis" publicKeyToken="4b01fa6e34db77ab"/>
<bindingRedirect oldVersion="1.9.2.27817" newVersion="1.9.3.19379" />
</dependentAssembly>
but still getting the same error. I check it with spy and it appears Calendar dll wants old API.
Any idea how to fix it? (Thanks)

Assembly version mismatch when performing unit-testing of Azure Mobile Services with PCL and Xamarin

Good day.
I want to create NUnit tests for my PCL xamarin library.
There are 1.1.0 Azure Mobile Service in xamarin components right now and 1.1.1 in NuGet
As i understand i need MobileServices.Ext for each platform, so i need 1.1.0 version for windows. Am i right? Is so, where can i find it?
I tried to add Microsoft.WindowsAzure.Mobile from xamarin Component store (version 1.1.0 in store, file version 1.0.0) and Microsoft.WindowsAzure.Mobile.Ext from Nuget (version 1.1.0)
here is exception
System.TypeInitializationException : Инициализатор типа
"RateIt.Backend.SAL.AzureWorker" выдал исключение. ---->
System.IO.FileLoadException : Не удалось загрузить файл или сборку
"Microsoft.WindowsAzure.Mobile.Ext, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null" либо одну из их зависимостей. Найденное
определение манифеста сборки не соответствует ссылке на сборку.
(Исключение из HRESULT: 0x80131040)
unit tests project needs this: Microsoft.WindowsAzure.Mobile.Ext, Version=1.0.0.0
as #TheESJ adviced it tried to add bindings
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.WindowsAzure.Mobile.Ext" publicKeyToken="null" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.1.0.0" newVersion="1.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
That doesn't help. Right now i found workaround: "linked project extension" with all files from PCL linked to project with tests, so i can use azure from NuGet and don't bother about PCL
You can provide a bindingRedirect in the app.config for the unit test and the unit test runner should honor this when loading the unit test assembly. Nuget does this automatically for applications, but not class libraries. If you have an application you can copy the binding redirects from there.
Version 1.1.0 of the NuGet package and Xamarin component had some issues when used together. As of Version 1.1.5 this is no longer the case, you should find they work together seamlessly.
Also, version 1.1.5 of the NuGet package supports Xamarin.iOS and Xamarin.Android targets, so if you are using Visual Studio only you can skip the component completely.
For more information see this tutorial.

Gallio error : MSTest executable was not found

I've got an annoying issue with Gallio when I try to analyse my VS2012 C# solution with my sonar-runner. When Gallio try to launch my unit test I can find this issue in the logs :
[error] Assembly XXXX
Cannot run tests because MSTest executable was not found
I've already tried some propositions of solution exposed here and here (I have installed Agents for VS 2012 and I have added a registry key with the path of my VS2012 installation) but nothing seems to work.
Thank you by advance for your help.
EDIT :
It seems that this issue come from an hard coded registry value in Gallio source code because when I try to add the InstallDir registry key for VS2010 to point to my VS2012 installation I can see a new error message.
This new error is an I/O Exception relative to the following DLL : "Microsoft.VisualStudio.QualityTools.CommandLine.dll" version 10.0.0.0 which I'm able to find in my GAC_MSIL directory but in version 11. My conclusion is that Gallio isn't fully compatible with VS2012 and the corresponding version of MSTest.
I'm going to investigate to find a way to manually generate Unit testing reports that Sonar will be able to store.
EDIT 2 :
There is finally no way to collect mstest reports in sonar for now. The only solution I found is to convert every unit test made with MSTest into an NUnit test to be able to run it with gallio and gather the results in my Sonar server.
Add the following to your machine.config located at C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.VisualStudio.QualityTools.CommandLine"
publicKeyToken="b03f5f7f11d50a3a"
culture="neutral" />
<bindingRedirect oldVersion="10.0.0.0"
newVersion="11.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
Then you will need to add a registry key called InstallDir with a value of
"InstallDir => C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\"
to the following location
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0
Then run the following command
regsvr32 "C:\YourSonarInstalation\opencover\x86\OpenCover.Profiler.dll
The issue is being discussed here. The potential solution above was posted a few days ago.
https://code.google.com/p/mb-unit/issues/detail?id=899
For Visual Studio 2013 and .net 4.5 This is a similar process.
Add the following to your machine.config located at C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config and/or C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.VisualStudio.QualityTools.CommandLine"
publicKeyToken="b03f5f7f11d50a3a"
culture="neutral" />
<bindingRedirect oldVersion="10.0.0.0"
newVersion="12.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
Then you will need to add a registry key called InstallDir with a value of "InstallDir => C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\" to the following location HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0
Then run the following command regsvr32 "C:\Program Files (x86)\OpenCover\x86\OpenCover.Profiler.dll"

Categories

Resources