Sirecore-IIS: BadImageFormatException: Bad Method Token

Sitecore is most likely not to be blamed here but I came across a weird error today when trying to run a Sitecore (8.1) website on my local machine. BadImageFormatException: Bad Method Token and the more common one “The type initializer for ‘CLASSNAME’ threw and exception”. What was weird was the error message contained a bunch of square boxes where you’d expect Class names and Method names. Earlier in the day, I had been working on a related but separate .NET solution. That project was targeting the .NET Framework 4.5 and my Sitecore solution was targeting 4.5.2. The other project needed to reference an assembly from my Sitecore solution so I had to update the Target Framework for the other solution to match the Sitecore one. Not sure if that had something to do with it. I tried the usual drill of restarting restarting Visual Studio, restarting IIS, and restarting the VM itself. Still no luck.

I had missed another of those usual things you do when you run into these kind of runtime issues; clean your ASP.NET temporary files. Once I cleaned my C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root everything was back to normal. It’s still a mystery but at least I can keep working.

YSOD Screenshot:

[BadImageFormatException: Bad Method token]
[TypeInitializationException: The type initializer threw an exception]
Sitecore.Nexus.Web.HttpModule.Init(HttpApplication app)