本主题包括 .NET WCF 数据流事务 与 SOAtest 的使用。

本章包含:

SOAtest 和 .NET WCF 数据流事务

SOAtest 可以使用 WS-Atomic Transaction 或 Microsoft OleTransactions 协议启动数据流事务。  当服务端点的绑定配置具有 transactionFlow="true" and either transactionProtocol="OleTransactions" or 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. 选择工具 Request 选项卡中的 SOAP Header 选项。



  2. 单击 SOAP Header 选项卡中的 Add 按钮。将显示 Add New SOAP Header 对话框。



  3. Add New SOAP Header 向导中选择 Custom ,然后单击 OK 按钮。将新行添加到 SOAP Header 选项卡。
  4. 双击添加到 SOAP Header 选项卡的新行。将显示一个 Edit 对话框。



  5. 将以下示例 OleTxTransaction SOAP header 粘贴到 Edit 对话框的 Literal/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. Edit 对话框的 Views 下拉菜单中选择 Form XML ,然后单击 Yes 以使用文本 XML 值覆盖表单 XML 值。
  7. 在表单 XML 视图中,选择 PropagationToken 元素,然后为选择 Parameterized 选项作为该元素的值。这将允许您选择包含传播令牌的 XML Data Bank 列。
  8. 单击 OK 按钮。

WS-Atomic 事务数据头

对于 WS-Atomic 事务,需要一个 CoordinationContext SOAP 数据头。类似于 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