Malachi
Malachi

Reputation: 2463

LINQ + WCF + Transactions

I have a relatively simple use case which is failing. Consider the following code:

[OperationBehavior(TransactionScopeRequired = true)]
public IEnumerable<StatusRecord> ReadActive(int contactID, bool isActive)
{
  var result = from n in ORM.Default.Table<StatusRecord>()
                where n.lng_contact_id == contactID && n.dte_effective_end == null
                select n;

  return result;
}

This is using a custom LINQ-SQL provider of our own evil origins. Normally this type of call works great, but when using it from a DTC Transacted WCF call, it hangs. My theory is that the serialization of the result into an array somehow occurs outside the transaction scope and therefore hangs. Furthermore, my theory is supported by the fact that changing the line

return result;

to

return result.ToArray();

makes things work. Although I am pleased to have a workaround, it seems there is a better way to get this behaving. Please advise. Thank you!

Upvotes: 2

Views: 177

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364409

Don't use deffered execution in WCF operation. You have to execute your query before you returning result - that is what you did in result.ToArray().

Upvotes: 1

Related Questions