如何在不中止整个事务的情况下处理 WCF 中的 FaultException? [英] How to handle a FaultException in WCF without aborting the whole transaction?

查看:27
本文介绍了如何在不中止整个事务的情况下处理 WCF 中的 FaultException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务作为事务的一部分被调用.

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

如果服务被调用两次(经常在调试期间发生)我希望服务抛出一个 FaultException 但整个事务必须成功.

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"));

既然我抛出了一个 FaultException,交易就会投票中止吗?

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;
   }
}

一个明显的解决方案是返回一个带有 Success 参数的消息——这是我最初所做的——但这是糟糕的设计,因为最终它是一个错误,应该有一个例外.

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.

允许 WCF 服务抛出错误但仍允许事务成功的最佳方法是什么?

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

推荐答案

您可以尝试将 WCF 服务的调用包装在具有事务范围选项 Supress(即您所拥有的事务范围的子事务范围)的事务范围中.这将阻止这部分代码的异常影响环境事务.只要在退出内部/子事务范围之前处理异常就可以了.

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

这篇关于如何在不中止整个事务的情况下处理 WCF 中的 FaultException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆