(转自saucer)
在企业开发里,经常遇到的一个问题是需要做distributed transactions。一般推荐的做法是做成servicedcomponent,参考
transaction control
writing serviced components
这种做法有几个问题,该组件以及所依赖组件必须是strong-named的,在通常的情形下(譬如web application里),我们需要手动注册组件。但这样,如果我们改动组件的话,需要停止服务,注销组件,然后重新注册组件。另外,这transaction是declarative和automatic的,无法用编码精确控制 transaction,而且declaration是在类的级别上的,无法在方法层次做 transaction。其原因是,servicedcomponent是基于早期com+的服务架构之上的,transactional context是与object(对象)密切结合在一起的。
但在几年前随xp推出的com+ 1.5 (windows 2003服务器也有支持)里, transactional context可以独立于object(对象)之外,极大地简化了transactional programming。下面这篇2002年msdn杂志上由com+专家tim ewald介绍了相关的api (coenterservicedomain/coleaveservicedomain),并且提供了c# wrapper 。
discover powerful low-level programming in windows xp with new com+ apis
在.net 1.1里,同样的功能是由2个类,system.enterpriseservices.servicedomain与system.enterpriseservices.serviceconfig,来实现的。
这里是don box去年7月12日的blog里的例子,
serviceconfig config = newserviceconfig();
config.transaction = transactionoption.required;
servicedomain.enter(config);
mytxcode();
servicedomain.leave();
在.net 2.0里,根据don,你可以这么做
using (transactionscope scope = newtransactionscope())
{
mytxcode();
scope.consistent = true;
}
也请参考
using distributed transactions in .net 1.x without deriving from servicedcomponent
