简体   繁体   中英

Azure Web app - Could not load file or assembly Newtonsoft.Json

We have a .NET 4.5 web app which runs without error on local machines, our local dev server, on an Azure VM (both 2008 r2 and 2012) without issue.

However, uploading the same application to Azure Web app we receive the following exception:

Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified. ->    at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
   at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
   at System.Web.Http.HttpConfiguration.DefaultFormatters()
   at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes)
   at System.Web.Http.GlobalConfiguration.<.cctor>b__0()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at System.Web.Http.GlobalConfiguration.get_Configuration()
   at MyHandler.Initialise()

Inside MyHandler.Initialise() we're calling the GlobalConfiguration.Configuration method (so we can add Routes).

If I dump the contents of the AppDomain assemblies just before the call I can see the newtonsoft assembly listed, with the same token also:

…
Newtonsoft.Json - Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
System.Net.Http.Formatting - System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
System.Web.Http - System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
System.Web.Http.WebHost - System.Web.Http.WebHost, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
…

What on earth is going on?

It's almost like there is some difference in the framework on Azure which is completely ignoring the NewtonSoft assembly.

I have seen posts about mismatched version of this assembly, but this doesn't appear to be the same issue – I'm running 4.5.0.0 and it is looking for the same version.

Try upgrading to the latest version of Newtonsoft, which is currently version 7.0.1, you are running 3 versions behind.

On this note, I have 4.5 deployed in a production environment right now in Azure Web Apps. Try:

  1. Ensuring you installed into your project via nugget
  2. Double check the publish package has the .dll and dependencies necessary for newtonsoft.

It sounds to me as though there are remnants of the a.DLL on those systems but not being deployed with your package to Azure. You can also try deploying to a completely new wiped box.

Try to clean your project or entire solution in Visual Studio. Then uninstall the existing Newtonsoft nuget package from your project, reinstall it again and try redeploy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM