【发布时间】:2016-12-02 10:11:57
【问题描述】:
目前,我正在做一个以微服务为主要概念的项目。
为了看得更清楚,我举个例子:
我得到了 Service A,它有自己的模型和控制器。
基本上,服务 A 只包含 数据库 A 的基本 CRUD 操作。
其次,我得到了Service B,与Service A 相同,但数据库不同(Database B)。
现在,我创建了 1 个服务来同时使用服务 A 和服务 B。目前我正在使用 TransactionScope 来“包装”事务,但它不起作用。
代码如下:
//This is the service to call Service A and Service B
using (TransactionScope ts = new TransactionScope())
{
callServiceAMethod(); // works good
callServiceBMethod(); // something happened, and failed
//from here I don't know what should I do
//What I'm expecting is : if one of the service i just called didn't work as expected,
//the transaction will be rolled back else will committed
}
任何帮助将不胜感激:)
【问题讨论】:
-
我认为您需要为每个服务创建事务,存储在某种数组中,如果出现故障,只需遍历所有
ts实例并回滚。 -
好的,也就是说,我需要为每个服务创建模型,对吧?有没有其他方法,所以我不需要为每个服务创建模型?
-
什么型号?对不起,真的不明白。您是否尝试将
TransactionScopeOption.Required添加到TransactionScope?您还需要使用嵌套事务,尽管不知道如何在服务调用其他服务方法的解决方案中实现它。请参阅Nested transactions- 19. requirement
标签: c# web-services wcf transactions microservices