2017-03-15 107 views
0

我无法获取transactionScope回滚,因为我错过了某些内容或者我误解了transactionScope的用途。带有异步wcf服务的TransactionScope

我有以下的方法是拨打电话两个WCF服务:

public async Task<IHttpActionResult> Put(IEnumerable<string> values) 
    { 
     using (TransactionScope scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) 
     { 
      Task task1 = service1.UpdateAsync(values); 

      Task task2 = service2.UpdateAsync(values); 

      await Task.WhenAll(task1 , task2); 

      scope.Complete(); 
     } 

     return Ok(); 
    } 

每个服务运行具有以下属性:

[OperationContract] 
[TransactionFlow(TransactionFlowOption.Allowed)] 
void Update(IEnumerable<string> values); 

的实际服务(两者相同)

public void Update(IEnumerable<string> values) 
{ 
    foreach (string value in values) 
    { 
     db1Access.Update(value); 
    } 
} 

SqlAccess(这是或我们正在使用的自己的库,基本上是你的面包和b这背后绝对IDbCommand的东西):

BaseSqlAccess sqlAccess = factory.CreateSqlAccess("stp_update"); 
sqlAccess.AddParameter("values", values); 

sqlAccess.ExecuteNonQuery(); 

合同的结合具有以下属性:

<wsHttpBinding> 
    <binding name="WSHttpCommonBinding" transactionFlow="true"> 
    </binding> 
</wsHttpBinding> 

API的配置实现服务端点:

<bindings> 
    <wsHttpBinding> 
    <binding name="CommonBindingConfiguration" transactionFlow="true" maxReceivedMessageSize="2000000"/> 
    </wsHttpBinding> 
</bindings> 
<client> 
    <endpoint address="http://localhost:56084/service2.svc" binding="wsHttpBinding" bindingConfiguration="CommonBindingConfiguration" contract="IService1" name="IService1EndPoint"/> 
    <endpoint address="http://localhost:56084/service2.svc" binding="wsHttpBinding" bindingConfiguration="CommonBindingConfiguration" contract="IService2" name="IService2EndPoint"/> 
</client> 

我下的如果我在Task.WhenAll之后抛出一个错误,它会开始回滚,我也尝试在service2中抛出错误,但service1仍然沿用并且没有回滚。

我在这里错过了什么?

有几件事情需要注意:

  • 数据访问正在执行的特效
  • 每个服务连接到两个独立的DB的
+0

糟糕对不起我的不好 – stuartd

+0

你可以在服务器端显示暗示端点的代码。这些方法也有相关的注释。也显示客户端的绑定配置。 –

+0

@ScottChamberlain我在API的配置数据中添加了实现服务端点的API – RichardMc

回答

1

我相信问题是,你有没有归因将TransactionScopeRequired的WCF服务实现为true。

[OperationBehavior(TransactionScopeRequired = true)] 
public void Update(IEnumerable<string> values) 
{ 
    foreach (string value in values) 
    { 
     db1Access.Update(value); 
    } 
} 

更多关于此herehere

+0

这似乎是从编码的角度对它进行了纠正,看起来我在服务器本身上仍然存在一些MSDTC配置问题。谢谢。 – RichardMc