Including Json.NET in a Visual Studio extension - json.net

I'm using JSON.NET in a Visual Studio extension and it's not included in the distribution (by design by Microsoft).
In the linked ticket they simply state that v9.0.1 should be used. And that supporting multiple VStudio versions "complicate things".
Another SO question gives and answer which doesn't work in all cases.
I use a nuget package which in turn depends on Newtonsoft.Json 10.0.x. Is there any way that I can continue to use JSON.NET v10.x without causing any trouble for Visual Studio?

One possible solution is to use AppDomain and wrap all Newtonsoft.json related call within new AppDomain:
myDomain = AppDomain.CreateDomain("myDomain", AppDomain.CurrentDomain.Evidence,
new AppDomainSetup()
{
ApplicationBase = extensionPath,
ConfigurationFile = Path.Combine(extensionPath, configurationFileName)
});
The file configurationFileName should be shipped with your extension and its content looks like:
<?xml version="1.0" encoding="utf-8"?>
<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-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

Related

FileLoadException after installing Ninject.MVC5

I am trying to learn ASP.NET MVC with Adam Freeman's "Pro ASP.NET MVC 5" book. Unfortunately all projects using Ninject throw the same error
An exception of type 'System.IO.FileLoadException' occurred in Ninject.dll but was not >handled in user code
Additional information: Could not load file or assembly 'System.Web.Mvc, >Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its >dependencies. The located assembly's manifest definition does not match the assembly >reference. (Exception from HRESULT: 0x80131040)
This is exactly the same problem which was discussed in this thread,
Issues after installing ninject mvc 3 in mvc 5 project
but the offered solutions don't work for me.
I have tried target platforms 4.5 and 4.5.1, Ninject MVC3 and MVC5. I have also inserted this snippet
<runtime>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</runtime>
in the Web.config file, without any effect.
Problem must be in the Ninject MVC3 and MVC5 packages. Whenever these packages are installed, any call to Ninject.StandardKernel() throws the exception, regardless if made from the Global.asaxor the new NinjectWebCommon.cs or from any other piece of code (of course, this should not matter for this kind of exception anyway, but in this thread Error using Ninject with ASP.NET V4 it was suggested that the error might have something to do with using the Global.asax for connecting Ninject to the application).
I have run out of ideas. Can anyone help?
Add to your Web.config (i.e. SportsStore.WebUI project):
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
It should work with this -> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

I am getting the Error
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)
for my CI build
Solution which I tried
<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>
It also did not work
In package manager console execute: Update-Package –reinstall Newtonsoft.Json.
UPDATE
I originally posted this as a comment but as #OwenBlacker suggested I'll just put it here:
If you still get an error after doing this, then what worked for me eventually is that I deleted Json.Net's <dependentAssembly> section from my .config file. Reinstall brings it back if it's not there and apparently you need to delete it. Until there will be a normal solution in the package itself, I'm afraid this manual step is a must.
Note: Please read the comments below before doing this.
As per René's comment below BE AWARE that the command posted in the answer will reinstall the package in every project in your solution. So if you use the Newtonsoft.Json package in several projects and maybe use different versions, just executing the above command might have unwanted consequences.
To everyone having problems with Newtonsoft.Json v4.5 version try using this in web.config or app.config:
<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>
IMPORTANT: Check that the configuration tag of your config file has no namespace attribute (as suggested in https://stackoverflow.com/a/12011221/150370). Otherwise, assemblyBinding tags will be ignored.
The key point is referencing right version in your config file.
Steps;
1- look at what is the version of your Newtonsoft.Json.dll in the project reference property what ever the version in your package folder (For example mine is 7.0.1 and the reference Version is 7.0.0.0)
2- look at what the project expect from you in the exception (mine is 6.0.0.0)
3- Add dependent assembly to your config file as it should be..
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="7.0.0.0"/>
</dependentAssembly>
I had no luck with any of the solutions presented here (uninstalling, reinstalling, deleting references, creating bindingRedirects etc.) I had to go back to an old version of Newtonsoft. Version 5.0.6 had been working before, so I tried that one. I had to enter these two commands in the Package Console:
uninstall-package newtonsoft.json -force
install-package newtonsoft.json -version "5.0.6"
The -force option in the first command is required to force the uninstall. Dependencies with other assemblies prevent the uninstall without it.
I fixed the problem adding this binding redirect to my .config file:
<runtime>
. . .
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
culture="neutral" />
<bindingRedirect oldVersion="4.5.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
The error message complains about not finding version 4.5.0.0, the current version of Newtonsoft.Json is 6.0.0.0 so the redirect should go from 4.5 to 6.0, not viceversa
I think you are pointing to the wrong target, change it to 4.5 instead of 6.0
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="4.5.0.0" />
</dependentAssembly>
This should work.
I've spend couple of days trying to resolve this frustrating issue. I've tried pretty much everything that can be found on the web. Finally I found that this error could be caused (like in my case) by the different target .Net project versions (4.5 and 4.5.1) in one solution. The steps bellow fixed it for me:
Double check the .Net version of every project that's in your solution. Just right click on project and go to Properties.
If possible set the same .Net version for all projects. If not at least try to change the Startup project one (for me this was the one causing the issues).
Remove all Newtonsoft.Json packs from the solution.
uninstall-package newtonsoft.json -force
Update all Newtonsoft.Json versions in all packages.config files, like so
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net451" />
Reinstall Newtonsoft.Json from "Package Manager Console" with:
install-package newtonsoft.json
Rebuild the solution
(Optional) 7. If you changed the Startup project, return it again
uninstall-package newtonsoft.json -force
install-package newtonsoft.json
Did the trick for me :)
if you using multiple project in same solution
and library of the one other
check is all projects has same version of Newtonsoft.Json
Remove the Newtonsoft.Json assembly from the project reference and add it again. You probably deleted or replaced the dll by accident.
I was writing a WebApi REST service client, so for me this error was caused by adding References to the System.Net.Http and System.Net.Http.Formatting assemblies manually via Add Reference, when I should have added the Microsoft.AspNet.WebApi.Client package via NuGet.
See also this answer to another question.
You have 2 different versions of JSON.NET library in your solution. To solve this you should upgrade them to latest version. Follow these steps:
1-Open solution explorer
2-Right Click on solution name
3-Select Manage Nuget Packages for Solution
4-Select Updates from menu
5-Update JSON.NET package
This will resolve your issue.
link:
Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies
Deploy the correct version to the CI machine
This is telling you that the assembly loader found a different version of the Newtonsoft.Json assembly, that does not match the reference you created in your project. To load the assembly correctly, you must either deploy the assembly side by side with your compiled code, or install the correct version of the assembly in the destination machine (i.e. in the GAC).
Alternative: make sure the configuration is in the correct file
If you want to keep the current solution, and load an assembly with a different version, make sure that the configuration you posted is in the correct .config file. Remember that there is no xpto.dll.config, a DLL loaded by an application always uses the config file of the running application.
Normally adding the binding redirect should solve this problem, but it was not working for me. After a few hours of banging my head against the wall, I realized that there was an xmlns attribute causing problems in my web.config. After removing the xmlns attribute from the configuration node in Web.config, the binding redirects worked as expected.
http://www.davepaquette.com/archive/2014/10/02/could-not-load-file-or-assembly-newtonsoft-json-version4-5-0-0.aspx
<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="7.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
Works for me.... just put the version you are using in newVersion i.e(newVersion="7.0.0.0")
I was getting same error and by adding below code error resolved on production.
Answer is too late but might help someone.
<runtime>
<assemblyBinding 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>
Close solution.
Open packages.config and *.csproj with text editor and delete any line have Newtonsoft.Json
Ex:
<Reference Include="Newtonsoft.Json,Version=9.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net40\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
Or
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net40" />
Open solution again and re-install Newtonsoft.Json by Install-Package Newtonsoft.Json
It work for me.
We had the exact same issue that you mentioned. We're using nunit to run tests through CI, and we have nunit running a file called tests.nunit, which describe a list of test dll fixtures to run.
Each test fixture had their own config file, but when run through the "tests.nunit" file the binding redirects seem to be ignored. The solution was to add the binding redirects to a new config file, "tests.config" that was beside the "tests.nunit" file.
I have got the same type of problem. And I also solved it just doing the following:
Go to TOOLS > NuGet Package Manager and Select Package Manager Console. Finally, execute the following two commands :)
uninstall-package newtonsoft.json -force
install-package newtonsoft.json
You should update the web.config file in the server.
When nuget install NewtonSoft update this file including this code
<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>
Just check the version of Newtonsoft.Json Newtonsoft properties
Then you need to add that version in your web config (in my case 8.0.0.0)
Web config
I made the mistake of adding a NewtonSoft .dll file for .Net 4.5.
My main project was 4.5, but when I added an extra project to my solution, it strangely added it as a .Net 2.0 project... and when I attempted to use NewtonSoft's 4.5 dll with this, I got this "Newtonsoft.Json couldn't be found" error.
The solution (of course) was to change this new project from .Net 2.0 to 4.5.
I was facing the same error and struggled with it for hours. I had a web API project which is using Newtonsoft.json and another UnitTest project for the web API project. The unit test project also needed the Newtonsoft.json reference. But on adding the link I was getting the above exception.
I finally resolved it by adding the below code snippet in the app.config of the unit test project:
<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>
In my case, the main project was still referencing an old version of Newtonsoft.Json which didn't exists in the project any more (shown by a yellow exclamation mark). Removing the reference solved the problem, no bindingRedirect was necessary.
I had the exact same problem with version 7.0.0.0, and the lib causing my problem was Microsoft.Rest.ClientRuntime which somehow was referring to the wrong version (6.0.0.0) of Newtonsoft.json, despite the right dependency management in nugget (the right version of newtonsoft.json (7.0.0.0) was installed).
I solved this by applying the redirection above from 6.0.0.0 to 7.0.0.0 (from Kadir Can) in the config file:
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="7.0.0.0" />
----> After a few days without changing anything it came up again with the same error. I installed version 6.0.0.0 n updated it to 7.0.0.0 it works fine now.
Reinstall newtonsoft package through nuget did not worked for me.
I had to manually call JsonConvert.DeserializeObject to bypass this issue
I changed
HttpResponseMessage response = await client.GetAsync(url));
response.EnsureSuccessStatusCode();
MyObject data = await response.Content.ReadAsAsync<MyObject>();
For
HttpResponseMessage response = await client.GetAsync(url));
response.EnsureSuccessStatusCode();
string jsonStr = await response.Content.ReadAsStringAsync();
MyObject data = JsonConvert.DeserializeObject<MyObject>(jsonStr);
In my case, after downloading the assembly and adding the reference to the project, I solved this by 'unblocking' the DLL before adding the reference to the project.
Using Windows explorer, browse to the DLL location, right-click on the DLL and then select 'properties'. You'll find an 'unblock' button on one of the tabs and then you can add the reference and the assembly will load correctly.
Nothing from above helped me, but what actually fixed it is the following:
Remove all dependency bindings in app.config (from all app.config files in the solution)
Execute the following command from "Package Manager Console"
Get-Project -All | Add-BindingRedirect
Rebuild
Reference:
http://blog.myget.org/post/2014/11/27/Could-not-load-file-or-assembly-NuGet-Assembly-Redirects.aspx
Right click your project select manage Nuget packages, type newtonsoft in the search box and install the latest version. Then Run your App
Another insidious problem is that it appears that binding redirects can just silently fail if the element has an incorrect configuration on any other dependentAssembly elements.
Ensure that you only have one element under each element.
In some instances, VS generates this:
<dependentAssembly>
<assemblyIdentity ...
<assemblyIdentity ...
</dependentAssembly>
Instead of
<dependentAssembly>
<assemblyIdentity ...
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity ...
</dependentAssembly>
Took me a long time to realise this was the problem!

How to find the wrong reference dll in website when debug

All, I am stuck in a problem which drive me crazy... please help to review it . thanks in advance.
I want to reference a NHibernate.dll to my website with the version number 2.1.0.4000.
But I didn't know why the website always to search the NHibernate.dll of version number 2.1.2.4000 when I try to debug it.
Here is what I try to do .
try to use fuslogvw to trace the assembly binding .
you can see the application want to bind the 2.1.2.4000 which is not my desired version.
remove all the reference of Nhibernate in my solution. and add the right one again.
To the website project. I found the dll I just added show auto update in the version number column instead of the right version number in the reference dialog. I don't why.
Could someone please help to give me some idea to figure out what happened to it . thanks. any comments are welcome.
updated
I searched with key text "NHibernate" in the Web.Config
the content about it is below.
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Iesi.Collections" publicKeyToken="aa95f207798dfdb4"/>
<bindingRedirect oldVersion="1.0.0.3" newVersion="1.0.1.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4"/>
<bindingRedirect oldVersion="2.0.1.4000" newVersion="2.1.0.4000"/>
</dependentAssembly>
</assemblyBinding>
<assemblyBinding appliesTo="v2.0.50727" xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Oracle.DataAccess" publicKeyToken="89b483f429c47342" processorArchitecture="amd64"/>
<bindingRedirect oldVersion="2.102.4.0" newVersion="2.112.2.0"/>
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Oracle.DataAccess" publicKeyToken="89b483f429c47342" processorArchitecture="ia64"/>
<bindingRedirect oldVersion="2.102.4.0" newVersion="2.112.2.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
updated
I checked dll in the Reflector, Anything wrong with it ?
Updated
The manifast of ILdasm shows below
Updated
Sometimes I found the log shows some clue which assembly reference the wrong dll.
you can see "Calling assembly: xxxx.dll, version=........."
It looks like its an issue with your dll file itself. The manifest of the .dll file has declared the version details in a wrong way. Try to download another copy of the file from some other resource. Now clean your solution, add a reference to it and rebuild the solution. The error should go.

Assembly binding redirect for Unity not working

I'm creating a new MVC 4 website, and I'd like to use the Unity.MVC3 library to integrate with the DependencyResolver stuff built into MVC.
I also want to reference some data-access DLLs from an older, much larger project.
My problem is that Unity.MVC3 and the older DLLs are compiled against different versions of Unity, 1.2.0.0 and 2.1.505.0, respectively. I tried creating a binding redirect in my web.config file like so:
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersions="1.2.0.0-2.1.505.0" newVersion="2.1.505.0" />
</dependentAssembly>
However, I still get the following error:
Could not load file or assembly 'Microsoft.Practices.Unity, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
I turned on assembly binding logging, and the last two lines state:
WRN: Comparing the assembly name resulted in the mismatch: Major Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
Why isn't my binding redirect being respected? Is there a way to override its checking for major version conflicts?
There is a typo in the key token:
<assemblyIdentity name="Microsoft.Practices.Unity"
publicKeyToken="31bf856ad364e35" />
should be:
<assemblyIdentity name="Microsoft.Practices.Unity"
publicKeyToken="31bf3856ad364e35" />
Binding redirect does not complain in case of typos, it just does nothing.
I've made a test application, with this configuration it works:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Practices.Unity" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.2.0.0" newVersion="2.1.505.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Pay attention to xmlns, without it, it fails silently.

DotNetOpenAuth with MVC 4.0

I have been trying out the DotNetOpenAuth samples with ASP.Net MVC 4 Developer Preview.
I can successfully invoke my Action from my test page, but run into a strange issue because of one line of code:
var request = _openid.CreateRequest(openIdUrl);
var fetch = new FetchRequest();
fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.First);
fetch.Attributes.AddRequired(WellKnownAttributes.Name.Last);
request.AddExtension(fetch);
//return RedirectToAction("Login");
return request.RedirectingResponse.AsActionResult(); // <-- This is the line throwing the error
If I comment out the offending line of code and uncomment the one before this, I do not see the runtime error anymore.
So far I have tried:
1) Ensuring that I have the correct redirects:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
</dependentAssembly>
</assemblyBinding>
<legacyHMACWarning enabled="0" />
</runtime>
2) Have the correct namespaces:
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.Extensions;
It seems to me that the DotNetOpenAuth dll was compiled against MVC V 1.0.0 and the binding redirect is either not working or the extension method was maybe working against a deprecated method.
MVC Version: 4.0.0.0
DotNetOpenAuth Version: 3.4.7.11121
Any help on getting this working with MVC 4 would be greatly appreciated.
The MVC error screen image is further below:
Image of Error Screen
Update
I found that AsActionResult is the cause of the issue, maybe because the extension method is not compatible with .Net 4.0. I can get the OutgoingWebResponse object from request.RedirectingResponse but do know how to cast it to an ActionResult
It appears your binding redirects are corrupted. Notice how System.Web.Mvc appears twice? Try removing the second one as the first one looks correct.
Yes, DNOA is built against MVC 1.0, and this is by design so that it works against all versions of MVC (given the appropriate redirects). This is purely an MVC version thing -- not a .NET 4.0 thing.

Resources