Random error: The operation is not valid for the state of the transaction.

Topics: General, Writing modules
Nov 16, 2011 at 8:55 AM
Edited Nov 16, 2011 at 8:56 AM

We have finished our website and want to bring it official live but unfortunately we're still getting the following error on our live server:

The operation is not valid for the state of the transaction.

I'm getting a little desperate now because our customer is complaining more and more about this error.

The following things i have tried:

  1. Don't cache contentitems but only viewmodels.
  2. MSDTC allowed on both the sql server as webserver.
  3. Tried medium trust and full trust

Non of these has fixed the problem? Does anybody has a clue?

Nov 16, 2011 at 12:16 PM

One thing you need to be careful of is passing any IQueryable or IContentQuery into a view. You should always use .ToList() etc. to ensure the query is executed in the controller or driver where it's generated, before you pass it into a view or dynamic shape.

I don't know for certain if that's the problem here but it certainly tripped me up a couple of times.

What I *think* happens is that after all the shapes are gathered for render, the controllers and drivers that they came from have finished their business and things start getting disposed - so depending on how keen the GC is being, you could find that something important (e.g. a TransactionScope, perhaps an IRepository, possibly a db connection) has been disposed and the query no longer works.

Nov 16, 2011 at 1:04 PM

ok thanks that is a good tip!

You know if passing a ContentItem to the view is a bad thing to do and do things in the view like: ContentItemOfUser.As<RoutePart>().Title  ?

Nov 16, 2011 at 1:48 PM

Orchard does have internal examples of doing that with content, so technically it shouldn't be a problem. However I think if you did that to access a part that hadn't yet been loaded from the database then you'd risk errors. For normal content display, all the parts are loaded when the drivers run; but if for instance you ran a ContentQuery and then immediately passed those items straight into a view, not all the parts would be expanded yet.