How to handle a FaultException in WCF without aborting the whole transaction?

拜拜、爱过 提交于 2019-12-11 03:22:34

问题


I have a WCF service being called as part of a transaction.

If the service is called twice (often happening during debugging) I want a FaultException to be thrown by the service but the overall transaction must succeed.

 throw new FaultException("Duplicate action not allowed for " + 
                          msgIn.ActionType, new FaultCode("DUPE_ACTION"));

Since I'm throwing a FaultException the transaction will vote to abort right?

I'm considering doing this (setting the transaction complete - and then throwing the fault) - but i don't even know if that will work. It also has some complications that I don't want to have to worry about.

[OperationBehavior(TransactionScopeRequired = true,
                   TransactionAutoComplete = false)]
public void MyMethod(...)
{
   try
   {
      OperationContext.Current.SetTransactionComplete( );
      throw new FaultException("Duplicate action not allowed for " + 
                          msgIn.ActionType, new FaultCode("DUPE_ACTION"));
   }
   catch
   {
      /* Do some error handling then */
      throw;
   }
}

An obvious solution is to return a message with a Success parameter - which is what I originally did - but that's bad design because ultimately it is a Fault and deserves to have an exception.

Or is the solution as simple as having my client catch the Fault and continue with the transaction. I'm concerned that voting has already taken place through.

What's the best way to allow a WCF service to throw a fault but still allow the transaction to succeed?


回答1:


You could try wraping the call to the WCF service in a Transaction Scope with Transaction Scope option Supress (That is a sub transaction scope to the one you have). This will stop an exception from this part of the code from affecting the ambient transaction. As long as you handle the exception before you exit the inner/sub transaction scope you should be OK.

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscopeoption.aspx



来源:https://stackoverflow.com/questions/1993592/how-to-handle-a-faultexception-in-wcf-without-aborting-the-whole-transaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!