4
votes

I'm currently managing an ASP.NET application for my company. Recently, I have been getting a System.TypeLoadException when I try debugging the code.

The exact message is:

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.

And it keeps pointing to the following region as the source of the error:

Line 21: new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
Line 22:
Line 23: HelpPageConfig.Register(GlobalConfiguration.Configuration);
Line 24: }
Line 25: }

The above is found in the help page configuration for Web API help content.

Has anyone had any of such issues?

UPDATE: This particular issue started the very moment I deleted the bin folder from TFS.

3
Did you recently change the .NET version used by the web site? - mason
No I didn't. The target .NET version has remained the same. - Oswald Umeh

3 Answers

4
votes

After over 24 hours of tireless research (and worrying) I resolved the issue.

Seeing that I kept on throwing a System.TypeLoadException, I researched around that type and found the several reasons why that exception gets thrown.

In my case it was because the particular version of Newtonsoft.Json.dll it needed wasn't what was being referenced, as it was stubbornly pointing to the wrong package folder that held the version meant for .NET 2.0 and 3.5. After removing those packages, I removed the referenced and added it again from the right package folder, cleaned the solution and built and wa-la it was back up and fine.

1
votes

Those are not the lines that are causing the exception, you see those lines referenced because the HelpPageConfig.Register(GlobalConfiguration.Configuration) is where the code that causes the exception is run. The actual exception is happening somewhere deeper in your Register method on the HelpPageConfig class.

Most likely though, you have another class that is inheriting from JsonContractResolver that is marked as internal. I can't tell you exactly, obviously, without seeing all of your code. But do a quick search across the files for a class that is inherting from that base class and go from there.

0
votes

I upgraded Microsoft.AspNet.WebApi to version="5.2.7" from version="4.0.30506.0"and updated newtonsoft to 7.0.1.
It is due to a mix of versions in the dependencies. Thanks for the pointer.