Getting collection was modified enumeration operation error in Linq2Umbraco
I am using Linq2Umbraco to retrieve data. But sometimes I get an error saying:
"collection was modified enumeration operation"
and the only way to resolve the error is to restart the IIS.
The error appears in different places in my code, but always when retrieving something through Linq2Umbraco.
I cache Linq2Umbraco for performance. In order to keep the cached LinqContext fresh, I remove the context from the cache when publishing a document (see code below). I haven't been able to recreate the error, but it just happens sometimes one my server. At first I thought it might have something to do with publishing a node, which might cause the Linq2Umbraco-context to be out of sync, but I haven't been able to prove that. Any ideas why ?
It's because the LinqToUmbraco data context is not threadsafe. So, if one request hits a MyDocType collection, it will lazy load it. However, if (while lazy loading) another request/thread hits the same collection, it will try to load the collection again, which will result in this exception.
I have not found a solution except to create one context per request. This has the added advantage of keeping your data fresh.
Having said that, I am getting this exception (more occasionally) when not caching the context at all...soooo...I dunno.
Getting collection was modified enumeration operation error in Linq2Umbraco
I am using Linq2Umbraco to retrieve data. But sometimes I get an error saying:
"collection was modified enumeration operation"
and the only way to resolve the error is to restart the IIS.
The error appears in different places in my code, but always when retrieving something through Linq2Umbraco.
I cache Linq2Umbraco for performance. In order to keep the cached LinqContext fresh, I remove the context from the cache when publishing a document (see code below). I haven't been able to recreate the error, but it just happens sometimes one my server. At first I thought it might have something to do with publishing a node, which might cause the Linq2Umbraco-context to be out of sync, but I haven't been able to prove that. Any ideas why ?
thanks
Thomas
It's because the LinqToUmbraco data context is not threadsafe. So, if one request hits a MyDocType collection, it will lazy load it. However, if (while lazy loading) another request/thread hits the same collection, it will try to load the collection again, which will result in this exception.
I have not found a solution except to create one context per request. This has the added advantage of keeping your data fresh.
Having said that, I am getting this exception (more occasionally) when not caching the context at all...soooo...I dunno.
Ah ha! I have a solution. You can lock each of the properties ('trees') on your data context by extending the partial data context class.
For you, it would look like this:
I've load tested this while republishing the site and individual nodes, and it appears that the threading exceptions go away! Hurrah!
is working on a reply...