本主题涵盖 .NET WCF 流动事务与 SOAtest 的使用。

章节目录:

SOAtest 和 .NET WCF 流动事务

SOAtest 可以使用 WS-Atomic Transaction 或 Microsoft OleTransactions 协议启动流动事务。当服务端点的绑定配置有 transactionFlow="true" 以及 transactionProtocol="OleTransactions"transactionProtocol="WSAtomicTransactionOctober2004" 时,即可使用流动事务。

如果 Web 服务的操作契约使用 TransactionFlow 属性,该属性带 TransactionFlowOption.Allowed 属性,则在 SOAtest 中启用事务是可选的,并且不需要任何额外的测试配置。

然而,如果操作契约使用的是 TransactionFlowOption.Mandatory 属性,则需要事务,并且必须配置测试套件来启动流动事务。有关 WCF 流动事务的一般信息,请参阅以下内容:

上面的 WS 事务流 MSDN 文章介绍了对于配置 WCF web 服务客户端来启动流动事务的要求。在 SOAtest 中配置事务所需的步骤与本文描述的类似。强烈建议用户阅读 WS 事务流文章,以便首先理解 WCF 中事务背后的基本概念。

配置 MSDTC

必须在运行 SOAtest 的机器以及承载 Web 服务的机器上正确配置 Microsoft 分布式事务协调器(MSDTC)。MSDN WS 事务流文章介绍了如何配置 MSDTC,以及如何配置 Windows 防火墙以允许 DTC。下面是在配置 MSDTC 时可能有用的其他一些文章。

管理测试套件

在事务范围内调用几个 Web 服务操作需要配置一个 TransactionScope 对象。以下面的 C# 代码片段为例:

CalculatorClient client = new CalculatorClient("transactionEnabledEndpoint");using (TransactionScope tx = new TransactionScope(TransactionScopeOption.Required)) {
	client.add(1, 2); 
	client.add(2, 3); 
	tx.Complete();
}
client.Close();

在 SOAtest 中,一个等效的测试套件的结构如下:

  • 扩展工具 1:创建事务范围
    • XML 数据库:传播令牌
  • 工具 1:add(1,2)
  • 工具 2:add(2,3)
  • 扩展工具 2:完成事务

在上述的测试套件汇总,需要扩展工具 1 来创建 TransactionScope 对象,以获取当前事务的传播令牌。然后将传播令牌放入 XML 数据库中,因为稍后工具将需要它(稍后将进行描述)。这是示例 Jython 代码可用于扩展工具 1:

from webtool.soap.wcf import *; 
from soaptest.api import *;
 
def createTransaction(input, context):
   id = WcfUtil.createTransactionScope(WcfUtil.TRANSACTION_SCOPE_OPTION_REQUIRED, WcfUtil.ISOLATION_LEVEL_SERIALIZABLE, 60000)
   context.put("MY_TRANSACTION_ID", id)
   token = WcfUtil.getCurrentPropagationToken()    
   return SOAPUtil.getXMLFromString([token])

测试套件中的扩展工具 2 用于完成事务。该扩展工具在上面的 C# 代码片段中执行与 tx.Complete() 相同的操作。以下是扩展工具 2 的 Jython 代码:

from webtool.soap.wcf import *;
def endTransaction(input, context):
   id = context.get("MY_TRANSACTION_ID")   
   WcfUtil.completeTransactionScope(id)

WcfUtil 方法和相应的 .NET API 描述如下所示:

int webtool.soap.wcf.WcfUtil.createTransactionScope(int scopeOption, int isolationLevel, int scopeTimeout)

参数

scopeOption

  • 参阅 TransactionScopeOption 枚举:http://msdn.microsoft.com/en-us/library/system.transactions.transactionscopeoption.aspx

  • 可以是以下其中之一:

    • WcfUtil.TRANSACTION_SCOPE_OPTION_REQUIRED
    • WcfUtil.TRANSACTION_SCOPE_OPTION_REQUIRES_NEW
    • WcfUtil.TRANSACTION_SCOPE_OPTION_SUPPRESS

isolationLevel

  • 参阅 IsolationLevel 枚举:  http://msdn.microsoft.com/en-us/library/system.transactions.isolationlevel.aspx

  • 可以是以下其中之一:

    • WcfUtil.ISOLATION_LEVEL_SERIALIZABLE
    • WcfUtil.ISOLATION_LEVEL_REPEATABLE_READ
    • WcfUtil.ISOLATION_LEVEL_READ_COMMITTED
    • WcfUtil.ISOLATION_LEVEL_READ_UNCOMMITTED
    • WcfUtil.ISOLATION_LEVEL_SNAPSHOT
    • WcfUtil.ISOLATION_LEVEL_CHAOS
    • WcfUtil.ISOLATION_LEVEL_UNSPECIFIED

scopeTimeout

  • TransactionScope 的超时时间(以毫秒为单位)。

返回

一个可以用来引用 TransactionScope 的 ID。

String WcfUtil.getCurrentPropagationToken()

返回

当前事务传播令牌的 Base64 编码。

WcfUtil.completeTransactionScope(int transactionScopeRef)

参数

transactionScopeRef

  • WcfUtil.createTransactionScope() 返回的 id

配置事务头

每个工具都需要一个包含传播令牌的 SOAP 头。此传播令牌来自链接到前面描述的第一个扩展工具测试的 XML 数据库。所需的 SOAP 头类型取决于事务协议。 

OleTransaction 头

对于 OleTransactions,需要一个 OleTxTransaction SOAP 头。  为了方便起见,这里有一个 OleTxTransaction SOAP 头示例:

<OleTxTransaction b:Expires="59904" s:mustUnderstand="1"
   xmlns="http://schemas.microsoft.com/ws/2006/02/tx/oletx"    
   xmlns:b="http://schemas.xmlsoap.org/ws/2004/10/wscoor"
   xmlns:s="http://www.w3.org/2003/05/soap-envelope">
   <PropagationToken></PropagationToken>
</OleTxTransaction>

若要将上述数据头添加到工具 1,请完成以下操作:

  1. 选择工具请求选项卡中的 SOAP Header 选项。
  2. 点击 SOAP Header 选项卡内的添加。将打开添加新的 SOAP Header 对话框。
  3. 选择 添加新的 SOAP Header 对话框中的 Custom 并点击 OK。将新行添加到 SOAP Header 选项卡。
  4. 双击添加到 SOAP Header 选项卡的新行。将打开编辑对话框。
  5. 将以下示例 OleTxTransaction SOAP 头粘贴到编辑对话框的字面量/XML 文本字段:

    <OleTxTransaction b:Expires="59904" s:mustUnderstand="1"
       xmlns="http://schemas.microsoft.com/ws/2006/02/tx/oletx"
       xmlns:b="http://schemas.xmlsoap.org/ws/2004/10/wscoor" 
       xmlns:s="http://www.w3.org/2003/05/soap-envelope">
       <PropagationToken></PropagationToken>
    </OleTxTransaction>
  6. 编辑对话框的输入模式下拉菜单中选择表单 XML,然后点击以使用字面量 XML 值覆盖表单 XML 值。
  7. 在表单 XML 视图中,选择 PropagationToken 元素,然后为元素值选择参数化选项。这将允许您选择包含传播令牌的 XML 数据库列。
  8. 然后点击 OK

WS-Atomic 事务头

对于 WS-Atomic 事务,需要一个 CoordinationContext SOAP Header。类似于 OleTxTransaction 头,PropagationToken 元素必须参数化来包含来自 XML 数据库的传播令牌。为了方便起见,这里有一个 CoordinationContext SOAP 头示例:

<CoordinationContext s:mustUnderstand="1"
   xmlns="http://schemas.xmlsoap.org/ws/2004/10/wscoor"
   xmlns:a="http://www.w3.org/2005/08/addressing"
   xmlns:mstx="http://schemas.microsoft.com/ws/2006/02/transactions"
   xmlns:s="http://www.w3.org/2003/05/soap-envelope">
   <wscoor:Identifier
xmlns:wscoor="http://schemas.xmlsoap.org/ws/2004/10/wscoor"></wscoor:Identifier>
   <Expires>59904</Expires>
   <CoordinationType>http://schemas.xmlsoap.org/ws/2004/10/wsat</CoordinationType>
   <RegistrationService>
	<Address
xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing">https://hostname/WsatService/Registration/Coordinator/Disabled/</Address>
	<ReferenceParameters xmlns="http://schemas.xmlsoap.org/ws/2004/08/addressing">
		<mstx:RegisterInfo>
			<mstx:LocalTransactionId></mstx:LocalTransactionId>
			</mstx:RegisterInfo> 
		</ReferenceParameters>
   </RegistrationService>
   <mstx:IsolationLevel>0</mstx:IsolationLevel>
   <mstx:LocalTransactionId></mstx:LocalTransactionId>
   <PropagationToken xmlns="http://schemas.microsoft.com/ws/2006/02/tx/oletx"></PropagationToken>
</CoordinationContext>

若要将上述头部添加到工具 2 中,请遵循与工具 1 相同的过程,但是要将上面的 CoordinationContext SOAP 头粘贴到编辑对话框中。

  • No labels