我正在尝试创建一个System.EnterpriseServices.ServicedComponent,以便参与分布式事务。我的main方法看起来像这样:
public void DoSomething()
{
try
{
// do something useful
// vote for commit
if (ContextUtil.IsInTransaction)
ContextUtil.MyTransactionVote = TransactionVote.Commit;
}
catch
{
// or shoud I use ContextUtil.SetAbort() instead?
if (ContextUtil.IsInTransaction)
ContextUtil.MyTransactionVote = TransactionVote.Abort;
throw;
}
}我尝试做的是检测分布式事务是否已中止(或回滚),然后继续回滚我的更改。例如,我可能在磁盘上创建了一个文件,或者做了一些需要撤销的副作用。
我尝试过在Dispose()方法中处理SystemTransaction.TransactionCompleted事件或检查SystemTransaction的状态,但没有成功。
我理解这是类似于“补偿”而不是“交易”。
我想做的事有意义吗?
发布于 2011-02-28 21:33:33
回答我自己的问题时,这也可以通过从System.Transactions.IEnlistmentNotification派生ServicedComponent来实现。
发布于 2012-02-11 07:49:53
我建议不要以这种方式管理事务,除非您需要它。
如果您希望您的操作在链中涉及的任何其他操作失败时投票中止,或者如果一切正常则投票支持提交;只需将[AutoComplete]属性(请参阅此article中的注释部分)放在方法声明的上方。
这样,当前事务将在异常引发的情况下中止,否则将自动完成。
考虑下面的代码(这可能是一个典型的服务组件类):
using System.EnterpriseServices;
// Description of this serviced component
[Description("This is dummy serviced component")]
public MyServicedComponent : ServicedComponent, IMyServiceProvider
{
[AutoComplete]
public DoSomething()
{
try {
OtherServicedComponent component = new OtherServicedComponent()
component.DoSomethingElse();
// All the other invocations involved in the current transaction
// went fine... let's servicedcomponet vote for commit automatically
// due to [AutoComplete] attribute
}
catch (Exception e)
{
// Log the failure and let the exception go
throw e;
}
}
}https://stackoverflow.com/questions/4413907
复制相似问题