The danger of intersecting Episerver content

Some time ago we had a rather elusive bug on one of my client’s Episerver websites: occationally an item would disappear from a menu, even though no editor involvement had been noted for at least 6 months. My collegue Svante Seleborg found the rather interesting cause of the problem.

Intersecting Episerver content

We had a line of code intersecting two collections of pages, like this:

 menuCollection = collection1.Intersect(collection2);

The two collections, collection1 and collection2, were produced by two separate calls to Episerver’s ContentLoader.

The problem is that there is nothing to guarantee that it will return exactly the same object instance for two different calls, it all depends on caching. For instance, if the page cache has expired between these two calls, the second call will produce the same pages but as different object instances.

It all comes down to the Intersect method using a default comparer, which in turn would compare the object references instead of what was probably intended.