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)

Sitecore Fast Query Get-Item: Counter-intuitive Behavior

I am working on a Sitecore project that has products and resources fed from a PIM (Product Information Management) System called inRiver. inRiver sends the CRUD operations for it’s entities (ChannelNodes, Products, Resources, Files, etc.) to Sitecore through an HTTP Handler called inRiverDataManager. I was troubleshooting a piece of code in this inRiverDataManager handler for performance issues. Specifically, the lookup of an inRiver entity in sitecore was taking close to 20 seconds. With 200K+ entities including products and resource (pdfs, images, videos, etc.) mass updates were taking days to complete. We needed to make some optimizations to improve this. I started looking at ways to improve the item lookup time in Sitecore. The Handler uses the fast query syntax using an attribute filter to get items from the Sitecore Master Database.

Now, ‘Sitecore Fast Query’ is supposed to be faster than ‘Sitecore Query’ but it wasn’t fast enough for our purposes. If you look at line #3 in the code snippet below you’ll see the path being built there and it looks like this when its built “fast://*[@_inRiver_Id=’201508′]”. 201508 is the Id of the entity from the inRiver System and the also the Name of the Item in Sitecore. After that seeing that I immediately thought, “aaah I can improve that by just giving it a more specific XPath. I was sure that something like this “fast:/sitecore/content/inRiver/387954//*[@_inRiver_id=’201508′]” would make the lookup faster. After all now it has to look in a sub-tree instead of the entire Sitecore content tree.

The results surprised me; it was actually 2-3 seconds slower than the non-specific XPath (see the Powershell ISE screenshot below). I think I need to do some SQL profiling to understand better as to how fast query translates to SQL. For the sake of completeness, I compared the look up times using an Id with both Plain Sitecore Query and Sitecore Fast Query and the results were as expected there.

In the end I ended up using the Content Search API (solr index) to lookup the Item which brought down the lookup time to be under 2 seconds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
///
/// This method gets called when inriver tries to update and entity in Sitecore though the HTTP Handler
///
internal void UpdateEntity(int entityId, int channelId)
{
     string path = "fast://*[@_inRiver_Id='" + entityId + "']";
     Stopwatch stopwatch = Stopwatch.StartNew();
     Entity entity = RemoteManager.DataService.GetEntity(entityId, LoadLevel.DataOnly);
     stopwatch.Stop();
     Utils.LogAction("Got entity with data. Took " + stopwatch.Elapsed, ApiType.InRiver, "UpdateEntity");
     stopwatch = Stopwatch.StartNew();
     Item[] items = this.MasterDatabase.SelectItems(path);
     Stopwatch.Stop();
     Utils.LogAction("Lookup items to update. Took " + stopwatch.Elapsed, ApiType.Sitecore, "UpdateEntity");
     ///
     /// more irrelevant code below this
     ///
}

Sitecore Powershell ISE Get-Item