Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global.Asax error in MVC4 Web API runnin in Azure compute emulator - only sometimes

I noticed yesterday that this error was happening in my local Windows Azure emulator when I debugged my web role. But the deployed project still worked on Azure. Today, my deployed project is failing with this same error. HELP PLEASE!

This code block in my Global.axax.cs file's Application_Start method:

GlobalConfiguration.Configuration.MessageHandlers
  .Add(new BasicAuthMessageHandler()
  {
     PrincipalProvider = new PrincipalProvider()
  });

throws this error:

Inheritance security rules violated by type: 'System.Net.Http.Formatting.JsonContractResolver'. Derived types must either match the security accessibility of the base type or be less accessible.

when I start debugging (this is an Azure web role project). But only sometimes (actually it seems to happen every OTHER time I debug) ... and all I have to do to fix it is to stop and re-start the debugger. What is causing the problem?

like image 395
Andrew B Schultz Avatar asked Nov 03 '22 10:11

Andrew B Schultz


2 Answers

This had something to do with the Newtonsoft.Json assembly. I deleted it from my project's references, cleaned and rebuilt the project (for good measure), and then re-added it and rebuilt the project again. The error went away.

System.Net.Http.Formatting.JsonContractResolver is derived from Newtonsoft.Json. Another thread (here: http://forums.asp.net/t/1812274.aspx/3/10) addressed this same problem by removing Newtonsoft.Json from the GAC, since it was there and System.Net.Http.Formatting was not. This apparently solved a person's problem, although it seems backward to me (why can't the base type be in the GAC while the derived type is not? Isn't the error saying the problem is the other way around??).

like image 144
Andrew B Schultz Avatar answered Nov 15 '22 13:11

Andrew B Schultz


Try updating your nuget packages. I updated mine, and it fixed the issue. I was also having this issue on azure service itself.

like image 41
Sean Sparkman Avatar answered Nov 15 '22 13:11

Sean Sparkman