Newton json version issue whie running webapi project - c#

I am getting this annoying error when running a webapi application. I am using newton JSON - version 9.01.
{"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)":"Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"}
My project .netFramework is 4.0. To help solve this issue I had added a dependency assembly:
<assemblyIdentity name="Newtonsoft.Json"
publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
and my project package.config
<package id="Newtonsoft.Json" version="9.0.0.0" targetFramework="net40" />
Both are equal but I am still getting error. Why is this happening? This is strange because my error message shows different assembly version. I have already updated newtonjson 9.0.0 and am using it, but the error throws differently.
I have tried installing newtonjson 6.0.0, and then the package manager console is throwing an error:
Unable to find version '6.0.1' of package 'Newtonsoft.Json'.
Sometimes when downgrading to a lower version newton json, C# dependency is not supported already or installed like it shows.
What should I do? I am very new in installing nuget pakages . Your help is appreciated.

in Package Manager Console:
Update in project: in pakage manager console close the targeting project and reinstall Newton soft and do a rebuild
Update-Package Newtonsoft.Json -Reinstall

Does your solution have multiple projects that target different versions of Newtonsoft.Json? Right-click the solution and Manage NuGet packages. If Newtonsoft.Json shows up in the Consolidate tab, then update the projects to use the same version.

Related

Unable to load System.Threading.Tasks.Extensions

I have a web project build on .net framework 4.5.1. We are trying to added PostgreSQL support for the project. Using Nuget, I have installed 4.0.4 npgsql to the project. Under references, I see the following being added to the project.
Npgsql - 4.0.4.0 - Runtime version v4.0.30319
System.Threading.Tasks.Extensions - 4.2.0.0 - Runtime version v4.0.30319
When I tried run the project and connect and get the data from the database, I am getting the following error saying FileNotFoundException:
System.TypeInitializationException
HResult=0x80131534
Message=The type initializer for 'com.rsol.RConfig' threw an exception.
Source=RConfig
StackTrace:
at com.rsol.RConfig.getInstance() in C:\Workspaces\PS\RConfig\RConfig.cs:line 1113
at RAdmin.Global.Application_Start(Object sender, EventArgs e) in C:\Workspaces\PS\RAdmin\Global.asax.cs:line 528
Inner Exception 1:
TypeInitializationException: The type initializer for 'com.rsol.Db.DbMgr' threw an exception.
Inner Exception 2:
FileNotFoundException: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
Inner Exception 3:
FileNotFoundException: Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
System.Threading.Tasks.Extensions which is installed using Nuget is not getting loaded to the project. When I checked the properties of System.Threading.Tasks.Extensions reference, the dll file exists in the location. I have also tried installing System.Threading.Tasks.Extensions.dll file to assembly using gacutil. I am still getting the same error.
Please let me know if you need any additional information.
Any help is really appreciated.
In my case, I got the issue after upgrading to version 4.5.4 and tried #user2713341 answer. It didn't work but put me in the right direction.
My project had no bindings for this library, so I added the binding and it worked
<dependentAssembly>
<assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
</dependentAssembly>
and it worked.
Note that it should be 4.2.0.1 even though the version is 4.5.4.
Update Nuget Package
https://www.nuget.org/packages/System.Threading.Tasks.Extensions/
will solve your problem
Update Nuget Package is not working for me.
What works?
In app.config need to change
<bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
To
<bindingRedirect oldVersion="0.0.0.0-4.5.4" newVersion="4.5.4" />
for current version 4.7.2 should work.
Microsoft like ;)
The response from #Keyjote was at the root of the solution for me, but rather than cherry-picking the assemblies, I was able to just reinstall. This seemed to automatically repair the app.config file.
Tools -> Nuget Packet Manager -> Packet Manager Console
Update-Package -reinstall -Project <your project name>
This way you don't need to mess with the syntax or have to figure out the PublicKeyToken values.
If you want to do it for the whole solution, you can omit the -Project <> parameter.
I got the error in a different context when trying to migrate using Entity Framework Core (EFC) Version 3.1.8 using the Package Manager Console. The project built successfully.
Trying the binding redirects as suggested in this thread and (re)installing different versions of System.Threading.Tasks.Extensions as well as EFC (as suggested also elsewhere) did not work for me.
I managed to solve the problem by
deleting the packages/ directory in my project's root directory
using manual NuGet Package Restore (link to Microsoft Docs) via Visual Studio
problem lies in *.csproj file. having wrong reference for System.Threading.Tasks.Extensions.4.5.4 because this dll not loading.
after referring correct framework version folder from lib its start working
old Reference:
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.2\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
corrected one:
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net472\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
please clean the nuget cache first, then run test case it will work
I have been struggling a lot with bindingRedirect.
I finally found a real gotcha that solved my problems.
I have a WCF server project in .net 4.8, depending on projects in .net standard 2.0.
I was updating Nuget packages in the referred projects.
I got al sorts of problems, I won't try to mention them here.
I had bindingRedirects in web.config. I had to rediscover that those were unnecessary, and maybe even conflicting, because of automatic generation of those into the dll.config. There are various aspects to make that work, see elsewhere.
But the real gotcha for me was that for IIS, I had to LINK web.config to the dll.config. The latter being the complete config-file, with all the bindingRedirects, which turned out to be working after all.
Try to download the package and add reference to your project explicitly . should work , I just resolved it .
I got a similar error message - but for a different reason. In packages.config set by NUGET manager There was a ref for the new version - but in project reference there was a ref for an old version. The solution - delete the ref in project reference

Could not load file or assembly Microsoft.Win32.Primitives, Version=4.0.0.0

I am having problems running my web application.
The project setup is ASP.NET Core web application (.NET Framework). I don't seem to get any Build error. The project doesn't just run.
System.IO.FileLoadException: 'Could not load file or assembly 'Microsoft.Win32.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)'
Try adding the System.Net.Http nuget package.
This happened to me after I had updated to Visual Studio 2017 15.4.5 and also updated a load of Nuget packages. I think part of the problem is also using .net framework 4.7.1 as well, which I installed last week...
The fix for me was to remove the dependent assembly from web.config, so just comment out or delete this section:
<dependentAssembly>
<assemblyIdentity name="Microsoft.Win32.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
In other for me to get it working i uninstalled the Microsoft.Win32.Primitives then installed the exact version which is 4.0. this worked fir me.
Note that there are higher versions but they did not work for me.
I accidently deleted my *.suo files and the problem fixed itself. Maybe it was an accident, or irrelevant but i just thought to share it.
If you are targeting .Net 4.7.1 project, I contend that a 4.7.1 versioned Primitives DLL is not available. Take a look at the properties in Nuget
I believe the Nuget expression would need a > not a = 4.6.
This feels like a hack, but it seemed to work. If you open Web.config, change this to 4.6

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

I am facing the error below
Could not load file or assembly 'Newtonsoft.Json, Version=7.0.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)
I could see the below in Web.config
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>
So I changed it to
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.1.0" />
</dependentAssembly>
In packeges.config I could see the below entry
But still I am facing the same issue. Please help
A lot of things can go wrong and this error message tells you nothing.
But still I am facing the same issue.
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
If you still get an error after doing this, then what worked for me eventually is that I deleted Json.Net's 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. In package manager console again execute:
Update-Package –reinstall Newtonsoft.Json
Also take a look at your .Net version of the projects in your solution.
This is the Microsoft solution with unloading the project.
I had the same issue. I followed ekostadinov's forced uninstall/reinstall steps, but needed to add one extra step:
I was upgrading my Solution to Framework 4.5.2. My old Web.Config file had a namespace in the configuration tag.
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
I updated to:
<configuration>
Then the bindingRedirect should work for whatever version of NewtonSoft you are using:
<runtime xmlns="">
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
</dependentAssembly>
</assemblyBinding>
This can happen if package.config contains 2 same packages name with a different version.
For Example,
<package id="System.Spatial" version="5.6.2" targetFramework="net45" />
<package id="System.Spatial" version="5.6.4" targetFramework="net45" />
Thank You.
I had the same issue and doing the uninstall and reinstall didn't help. At the time I was trying to install the most current version (10.0.3) of Newtonsoft.Json. I ended up installing the 7.0.1 version and then ran across another article that suggested copying that .dll to :\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE. Which I did.
Started the install REST API process again and then received a different error. When attempting to install Microsoft.Rest.ClientRuntime 2.3.2 it failed. For this, I just went into NuGet and had it install to the project in which I was installed the REST API.
Started the REST API install again and this time it installed.
Oh and if it helps anyone with searches, I was doing the Azure Immersion 02-API App tutorial using VS2015 on Windows Server 2012R2.
I had the same issue and I got the exception when I was trying to create MassTransit queues:
"Exception: System.TypeInitializationException: The type initializer for 'MassTransit.Serialization.JsonMessageSerializer' threw an exception. ---> System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=12.0.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)"
Solution that worked for me (after spending couple of days reverting several commits):
We had a windows service solution that has .Service project and
.XUnitTests project. Both of them were using a common nuget that has
dependency on Newtonsoft.Json.dll. There was no explicit reference to
Newtonsoft.Json nuget package in both projects (but we were using
'using Newtonsoft.Json;' namespace in our classes), so the common
nuget was using version 9 of Newtonsoft.Json by default.
As soon as I installed the Newtonsoft.Json nuget in both .Service and
.XUnitTests projects, the common nuget package started using the
latest v12 Newtonsoft and that fixed my issue.
Just posting it in here if it saves anyone their valuable time.
most of the time this error happens because of different assemblies are dependent on specific version of some other assembly.
the easiest fix is to update all packages that are dependent to Newtonsoft.Json using the nuget package manager.
it will automatically set all config files in your project.
I got the same error, and fixed it by doing a 'Clean' on my solution. I'm using Newtonsoft.Json in a UWP app.
Thanks #ekostadinov. I was able to get it working with the solution from #ekostadinov. In my case, the default Web MVC template had NewtonSoft JSON version 6. I had added a class library and to that I installed NewtonSoft JSON version 9 from Nuget package manager.
I tried removing the reference from my Web project, packages config and dependencies, but still I was getting error. Now, I was getting error message saying unable to find Newtonsoft JSON version 6. Initially I was getting error message saying unable to find Newtonsoft JSON version 9.
Then I followed #ekostadinov steps.
uninstall-package newtonsoft.json -force
install-package newtonsoft.json
I got a warning with respect to a dependency in the package manager console mentioning about a dependency with the version of Web.Grease dll. But anyways, it was successful.
Just to double check, I ran the last command mentioned in the above post
Update-Package –reinstall Newtonsoft.Json
It ran without any warnings as well.
Now, I am able to use version 9 in all my projects.
Well, in my case, there were multiple projects in the solution and each was using different version of NewtonSoft.json. I had to go ahead and change the version in each project's package.config to match with latest version(Well, it could be any version just that it should be same across the projects). Once all the package.config point to the same version, all I had to do was
Update-Package –reinstall Newtonsoft.Json
Now, watch out I also had to manually change the version on web.config assembly entry in one of the projects. but that did the job.
My be it is too late but hopefully, it might be useful for the people supporting old projects.
Add dependentAssembly tag and give the version number in oldVersion for which you are getting error.
In my case it was giving the error for version 6.0.0
Give the installed version value in newVersion as mentioned below:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>

FileLoadException was unhandled by user code

I am setting up the API for my MVC-4 app and when I uncommented this line in Globals.asax.cs:
WebApiConfig.Register(GlobalConfiguration.Configuration);
I received this exception when I started my project back up:
An exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll but was not handled in user code
Additional information: 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)
What should I do?
Update 1 (screenshots)
from what I can tell, JSON.Net looks like it is installed correctly.
Update 2
JSON.Net actually seems to work when the API routes are commented out in Globals.Asax. This doesn't throw any errors:
public ActionResult Index()
{
var foo = Newtonsoft.Json.JsonSerializer.Create();
return View();
}
Visual Studio only complains when this line is uncommented:
WebApiConfig.Register(GlobalConfiguration.Configuration);
This also occured to me today. Seems like there had been an update for json.net (now version 6.0.3), causing nuget to download the latest version after build. However references to old json.net libs might not get updated when there are depencies to other libs.
Solution: Manually open the manage nuget packages for solution window and uninstall old version(s) of json.net. Then take the latest version and install for all needed projects. That fixed the exact error you had for me...
-- edit --
Ok, so I found out that this solution worked for me locally, but remotely this did not solve my issues. Seems like there are some old dependencies from other libs hard referencing the 4.5.0.0 version of json.net. More topics on Stackoverflow.com provide the following solution.
Add this assembly binding redirect to your web.config file:
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="1.0.0.0-4.5.0.0" newVersion="6.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Most probably Newtonsoft.Json DLL is not properly deployed.
Make sure you have the Newtonsoft.Json DLL in your (IIS / project) bin folder.
Alternatively, you can also install that DLL to GAC if you plan to use it across multiple projects.
It looks like that you don't have Newtonsoft.Json installed/referenced. Web API relies on this and won't work correctly until you resolve this dependency. You can install it via NuGet.
Simply delete your Newtonsoft.Json dll from bin folder then open package.config file and remove your Newtonsoft.Json entry from there then reinstalled your Newtonsoft.Json by command but don't installed newer version if you face this problem with newer version find old version command
like Install-Package Newtonsoft.Json -Version 6.0.8
now Install-Package Newtonsoft.Json -Version 7.0.1 is also aviable but i suggest to you installed 6.0.8 version its working

System.Web.Http reference defaults to 4.0 version no matter how I try

I am using the BreezeApi NuGet package in my project. It is in Visual Studio 2013.
I get this error.
Error 41 Assembly 'Breeze.WebApi2, Version=1.4.0.0, Culture=neutral, PublicKeyToken=f6085f1a45e2ac59' uses 'System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' which has a higher version than referenced assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
I don't know why my project keeps referencing the 4.0 version. Things I tried:
I removed the reference of system.web.http 4.0 from my project and browsed to the 5.0 DLL file location. As soon as I add it and come back to Solution Explorer, right click on the system.web.http dll, it is showing 4.0.0.0.
I added assembly binding in web.config like below
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.1.0.0" newVersion="5.1.0.0" />
</dependentAssembly>
How do I resolve this?
This is what I did to solve an issue very similar to yours:
Error 2 Assembly 'Breeze.WebApi2, Version=1.4.0.0, Culture=neutral,
PublicKeyToken=f6085f1a45e2ac59' uses 'System.Web.Http,
Version=5.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
which has a higher version than referenced assembly 'System.Web.Http,
Version=5.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35' c:\Repos\SPACodeCamper\packages\Breeze.Server.WebApi2.1.4.12\lib\Breeze.WebApi2.dll CC.Web
I got this error while doing the course Building Apps with Angular and Breeze - Part 1 by John Papa. It happened on step 3.15 after adding the Breeze Web API Controller to CC.Web.
To solve it we need to update the following NuGet Packages. I used NuGet Package Manager in Visual Studio 2013:
Microsoft.AspNet.WebApi.Client from 5.0.0 to 5.1.2
Microsoft.AspNet.WebApi.Core from 5.0.0 to 5.1.2
Microsoft.AspNet.WebApi.WebHost from 5.0.0 to 5.1.2
Microsoft.AspNet.WebApi.OData from 5.0.0 to 5.1.2 (this one I had to restart Visual Studio to complete the installation)
Updating the NuGet packages above will take care of bumping System.Web.Http's version to the correct one, in this case 5.1.0.0.
I think your answer is given here.
You have to update the NuGet packages. The easiest one is update-package in the NuGet packet manager console.
Be aware of other dependencies that might break.
I solved this by updating all my packages from NuGet.

Categories