tags:

views:

113

answers:

2

Here's my scenario: I need to make three or four calls to different WCF services before completing the transaction - what are my options, if any?

ServiceA.SaveWork(work1);
ServiceB.SaveWork(work2);
ServiceC.SaveWork(work3);
ServiceD.SendNotification(notification);

If one call fails, all fail... Note that these services may not be in the same domain.

Cheers!

+1  A: 

You should be able to wrap those into a System.Transctions.TransactionScope to achieve this:

using(TransactionScope scope = new TransactionScope())
{
   ServiceA.SaveWork(work1);
   ServiceB.SaveWork(work2);
   ServiceC.SaveWork(work3);
   ServiceD.SendNotification(notification);

   scope.Complete();
}

Of course, you need to make sure your WCF services don't explicitly prevent being part of a transaction! (check out the TransactionFlow attribute - avoid the TransactionFlow.NotAllowed setting!)

marc_s
So far that looks like a sole option...
Ostati
That would involve the MSDTC, wouldn't it? If so...be prepared for a lot of pain in the hole....
Alex
@Alex: if it deals with multiple resources, yes. Granted, DTC is a pain, but often the only viable solution - that or no transaction support at all...
marc_s
+1  A: 

If these services are all on different machines etc, using two phase commit will lead to lots of real world problems.

Therefore I don’t think transactions are a good solution....

I think you need to make all your services so you can undo the work if needed to recover from an error. Undoing a item of work can be very complex in the real world.

E.g If you need to book a car and a hotel and then the hotel burns down, you can’t expect to be able to un-book the car without losing some money.

However if all the services sit on top of the same database, then transactions may work well for you.

Ian Ringrose
@Ian: can you say which problems you're expecting?
John Saunders
I'd love to know what are those real world problems, but yes, all the services sit on top of the same DB.
Ostati
@John, e.g the IT person that runs ServiceB in the USA office restores a database at a different time to the IT person that runs ServiceC for a different company that is based in the UK.
Ian Ringrose
@Ian: what's that have to do with transactions? That's more of a competence issue than a transactions issue.
John Saunders
@John, But the system design has to cope with the **real world**, in the real world you get lots of "competence issue" as soon as you do anything that makes use of services that are not all controled by a single person.
Ian Ringrose
@Ian: most places I've worked handle the kind of issue you're describing by putting good procedures in place and by firing staff who violate such procedures. I also don't believe the issue ou're describing is a real one. Certainly, I've never heard of it in over 30 years in the industry.
John Saunders
Everything aside, I think adding the "Undo" method to the service will face the same problem - what if something goes wrong during ServiceA.UndoWork(work)?
Ostati