Error Using JSON.NET - c#

I am trying to use JSON.NET and after including the .dll and trying to use one of the methods I get this error:
Could not load file or assembly 'Newtonsoft.Json.Net35, Version=4.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
Any know why I might be this error?

Two things to check:
(1) You may have to "Unblock" the DLL. By default, when you download a .zip file from the Internet, that file, and all .dll or .exe files extracted from that .zip file, are given a file system attribute that prevents them from loading and executing. Right-click on the DLL in Windows Explorer, choose "Properties", and in the resulting dialog box click on the "Unblock" button. Or better yet, do that for the .zip file, and then re-extract all the files.
(2) The Newtonsoft JSON.NET library comes in five flavors: one each for .NET 2.0, 3.5, 4.0, Silverlight, and Windows Phone. You need to use the right one for your particular environment. I presume that this is a .NET 3.5 project?

In my case, I resolved this problem once I realized that a library that I was using was itself using the Json.NET but with the earlier version (3.5). Linking the second library to the new Json.NET version solved the issue.
Hope this helps.

You need to download release 1 instead of release 2 of the Newtonsoft.Json.

try to add assembly binding redirect to app config 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-4.0.0.0" newVersion="4.5.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Related posts:
System.IO.FileNotFoundException
At least one module has an unresolved import
Debugging tests that require an external dll

Related

Windows Service Exception - Could not load file or assembly Newtonsoft.Json

I am using Newtonsoft.Json in my Windows Service project.
My Newtonsoft.Json version is 6.0.0.0 and I have it referenced in my Project's References.
Installation and compilation go through just fine. But when I start my service from Services it throws an exception:
Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0,
Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its
dependencies. The system cannot find the file specified.
I also searched the web and added the following in my app.config file:
<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>
But still getting the same error.
What am I doing wrong?
If you are planning to convert array into Json, no need to use outsourced dll's. VS 2010 has JavaScriptSerializer to approach this task.
The example is as follows:
using System.Web.Script.Serialization;
JavaScriptSerializer js = new JavaScriptSerializer();
var json = js.Serialize(strYourArrayString);
Probably the problem is related to startup directory (is 'Newtonsoft.Json' in GAC?).
You can try to work on services to set the directory but I gave up.
Actually I set always copy local for non Microsoft referenced assemblies and I use a source code similar to this
How to add folder to assembly search path at runtime in .NET?

Use a specific version of a dll in C#

I have a project where I would want to use some specific version of a dll.
The GAC contains couple of versions of that dll (new & old), I would want to use the old when running the program.
Issue is that the newest dll is always picked-up from the GAC.
Would you know if there is a way to either:
Force the usage the dll that is in the run folder (the one I'm referencing in my solution, working fine in debug).
Force the usage of the old version of the dll from the GAC.
Thank you!
You can use a binding redirect in your app.config or web.config in the runtime node:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Make sure you have the correct publicKeyToken and know which versions you want to redirect to what version.
(You can check a publicKeyToken of a DLL like this with this info.)
MSDN Documentation
You can also generate these for an entire solution using the Package Manager Console
Get-Project -All | Add-BindingRedirect
This will update all app.config files and add the binding redirect.
When you have added the library to your project and you collapse the 'References'-node of the project tree, you'll see the added library. When you select it and click the 'Properties'-node of the context menu, you can specify if a specific version of the library should be used and which version to use. Simply set 'Specific Version' to true and specify the Version number. Then you don't have to cope with the question where the version you want is loaded from.
Have you try to "Redirecting Assembly Versions" in your app.config? http://msdn.microsoft.com/en-us/library/7wd6ex19(v=vs.110).aspx

C# asp.net Supporting multiple versions of a dll

I have the following dll hell:
a ASP.Net project
references WebGrease
which references Antlr3.Runtime.dll 3.3.1.7705 [stored in /bin/ folder of the asp.net app]
references Custom project
which references NCalc.codeplex.com
which references Antlr3.Runtime.dll 3.1.3.22795 [stored in /bin/CustomProject/ folder of the asp.net app]
unsurprisingly these two version of Antlr are not working well together and I get "The located assembly's manifest definition does not match the assembly reference" errors
I am unwilling to modify the WebGrease project.
I am attempting to upgrade the NCalc project to use 3.3.1.7705 however I am struggling with this
Do you have any suggestions on how to get these two DLL's to work together?
EDIT unfortunately the NCalc code is not compatible with the newer version of antlr so I cannot used binding redirects
Thank you
Providing NCalc can use the later version of Antlr3.Runtime - ie there are no breaking changes you should be able to use a binding redirect to direct it to load the later version
eg in the web.config file ass something like
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="NCalc" publicKeyToken="xxxxxxxxx" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.1.3.0" newVersion="3.3.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
I don't think the 4th digit on the versions is used

Could not load file or assembly 'ABC.dll, Version=5.5.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies

We have an C# class library project which was created using .Net framework 3.5.In that C# project we add an assembly ABC.dll build using VS 2008 3.5 framework with version 5.5.0.0.
Now when we add ABC.dll with an updated version of 6.6.2.1 then in Designer view still get an error:
Could not load file or assembly 'ABC.dll, Version=5.5.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
I am really stuck and cannot find the reason in-spite of adding 6.6.2.1 why the project is asking for 5.5.0.0 version dll?
We are now using C# Express Edition 2010 to open the project.
Make sure that the reference is present and any dependent assemblies are referenced appropriately; you may need to make sure that the references have the property "SpecificVersion" set "false".
If the assemblies are present and the correct version - try cleaning the solution (Build | Clean Solution), as sometimes old versions linger in the project output folders and confuse Visual Studio.
You should be able to add an app.config file, telling your library which file to use when the .dll is called.
This should contain something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="ABC"
publicKeyToken="8fe83dea738b45b7"
culture="neutral"/>
<bindingRedirect oldVersion="5.5.0.0"
newVersion=" 6.6.2.1"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

How can you make a c# project ignore the version number of an assembly?

I am working on a project that references dlls from another product. The product has a release each year and the assemblies version changes for each one, although the methods stay the same.
When I run a build of my project for 2010 when I try and run it for 2009 it throws an error because it is dependent on a different version. Is there a way around this?
If you're referring to a problem at runtime after swapping versions of your assembly without performing a rebuild of the program referencing your newly built assembly, you'll want to use a <bindingRedirect> directive to your program's App.config (or Web.config, if you're talking about a web site.)
bindingRedirect is used to instruct the .NET Framework that it's OK to use a version of an assembly other than the one the application was originally compiled against. By default, the CLR wants to see the same version of a DLL that your application was referencing during build, and if it doesn't it will throw an exception.
Try selecting the reference, and in property window set Specific Version as false.
It is possible to map different .net version of assembly in app.config that you put in application root folder
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Waters.ACQUITY.Remote"
publicKeyToken="6c13fd0b3604de03"
culture="neutral" />
<bindingRedirect oldVersion="1.40.0.0"
newVersion="1.60.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
This is solution when assembly you have referenced has references inside it to another specifc library version.
It happens when at compilation time "Specific version" is set to true. To avoid this problem it should be false.

Categories