如何在不中止整个事务的情况下处理 WCF 中的FaultException?
我有一个 WCF 服务作为事务的一部分被调用。
如果服务被调用两次(通常在调试期间发生)我希望服务抛出一个FaultException,但整个事务必须成功。
throw new FaultException("Duplicate action not allowed for " +
msgIn.ActionType, new FaultCode("DUPE_ACTION"));
由于我抛出了一个FaultException,事务将投票决定中止,对吗?
我正在考虑这样做(设置事务完成 - 然后抛出错误) - 但我什至不知道这是否有效。它还具有我不希望出现的一些并发症担心。
[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
参数的消息 - 这就是我最初所做的 - 但这是一个糟糕的设计,因为最终它是一个错误,应该有一个例外。
或者解决方案是否像让我的客户捕获故障并继续交易一样简单。我担心投票已经通过了。
允许 WCF 服务抛出错误但仍允许事务成功的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试将对 WCF 服务的调用包装在具有事务范围选项 Supress 的事务范围中(这是您所拥有的事务范围的子事务范围)。这将阻止这部分代码的异常影响环境事务。只要在退出内部/子事务范围之前处理异常,就应该没问题。
http://msdn.microsoft.com/en-us/库/system.transactions.transactionscopeoption.aspx
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