Step by Step WCF—Transactions

Step by Step WCF—Transactions

在本次实验中,你将会和一个银行的程序打交道。通过这个程序,你将会看到如何加入transaction。首先你需要创建一个数据库。打开Transactions文件夹,使用Bank.sql脚本创建数据库。

打开Bank.sln解决方案。想往常一样,解决方案中包含了服务端和客户端的程序。我们先来看服务端。服务端包含了AccountService和AccountManger两个服务。AccountService实现了IAccount接口,用于完成借贷功能:

Step by Step WCF—Transactions
[ServiceContract]
Step by Step WCF—Transactions
interface

 IAccount
Step by Step WCF—TransactionsStep by Step WCF—Transactions
Step by Step WCF—Transactions

{

Step by Step WCF—Transactions   [OperationContract]
Step by Step WCF—Transactions 
void Credit(int accountNumber,decimal
 amount);
Step by Step WCF—Transactions
Step by Step WCF—Transactions   [OperationContract]
Step by Step WCF—Transactions   
void Debit(int accountNumber,decimal
 amount);
Step by Step WCF—Transactions}


Step by Step WCF—Transactions[ServiceBehavior(InstanceContextMode 

=
 InstanceContextMode.PerCall)]
Step by Step WCF—Transactions
class

 AccountService : IAccount
Step by Step WCF—TransactionsStep by Step WCF—Transactions
Step by Step WCF—Transactions

{

Step by Step WCF—Transactions   
public void Credit(int accountNumber,decimal
 amount)
Step by Step WCF—TransactionsStep by Step WCF—Transactions 
Step by Step WCF—Transactions
{

Step by Step WCF—Transactions      BankAccountsTableAdapter adapter 
= new
 BankAccountsTableAdapter();
Step by Step WCF—Transactions      BankDataSet.BankAccountsDataTable accounts 
=
 adapter.GetData();
Step by Step WCF—Transactions
Step by Step WCF—Transactions      BankDataSet.BankAccountsRow account 
=
 accounts.FindByNumber(accountNumber);
Step by Step WCF—Transactions      account.Balance 
+=
 amount;
Step by Step WCF—Transactions      adapter.Update(accounts);
Step by Step WCF—Transactions   }

Step by Step WCF—Transactions 
public void Debit(int accountNumber,decimal amount)
Step by Step WCF—TransactionsStep by Step WCF—Transactions 
Step by Step WCF—Transactions
{

Step by Step WCF—Transactions      BankAccountsTableAdapter adapter 
= new
 BankAccountsTableAdapter();
Step by Step WCF—Transactions      BankDataSet.BankAccountsDataTable accounts 
=
 adapter.GetData();
Step by Step WCF—Transactions
Step by Step WCF—Transactions      BankDataSet.BankAccountsRow account 
=
 accounts.FindByNumber(accountNumber);
Step by Step WCF—Transactions
Step by Step WCF—Transactions      
if(account.Balance >=
 amount)
Step by Step WCF—TransactionsStep by Step WCF—Transactions      
Step by Step WCF—Transactions
{

Step by Step WCF—Transactions         account.Balance 
-=
 amount;
Step by Step WCF—Transactions      }

Step by Step WCF—Transactions      
else
Step by Step WCF—TransactionsStep by Step WCF—Transactions      
Step by Step WCF—Transactions{

Step by Step WCF—Transactions         
throw new InvalidOperationException(Debit amount is greater than balance in account # +
 accountNumber);
Step by Step WCF—Transactions      }

Step by Step WCF—Transactions      adapter.Update(accounts);
Step by Step WCF—Transactions   }

Step by Step WCF—Transactions}


Step by Step WCF—Transactions
Step by Step WCF—Transactions

代码不是很复杂,这里就不讲解了。
配置文件对AccountService暴露了两个endpoint,一个使用TCP、一个使用HTTP:

Step by Step WCF—Transactions<service name = “AccountService”>

Step by Step WCF—Transactions

<
endpoint
Step by Step WCF—Transactions   
address  = “net.tcp://localhost:8001/AccountService/”


Step by Step WCF—Transactions   binding  

= “netTcpBinding”

Step by Step WCF—Transactions   contract 

= “IAccount”

Step by Step WCF—Transactions

/>

Step by Step WCF—Transactions

<
endpoint
Step by Step WCF—Transactions   
address  = “http://localhost:8002/AccountService”


Step by Step WCF—Transactions   binding  

= “wsHttpBinding”

Step by Step WCF—Transactions   contract 

= “IAccount”

Step by Step WCF—Transactions

/>

Step by Step WCF—Transactions

</service>

Step by Step WCF—Transactions

AccountManger类实现了IAccountManger接口,用来查询帐户:

Step by Step WCF—Transactions
[DataContract]
Step by Step WCF—Transactions
class

 Account
Step by Step WCF—TransactionsStep by Step WCF—Transactions
Step by Step WCF—Transactions

{

Step by Step WCF—Transactions   [DataMember]
Step by Step WCF—Transactions   
public string
 Name;
Step by Step WCF—Transactions
Step by Step WCF—Transactions   [DataMember]
Step by Step WCF—Transactions   
public decimal
 Balance;
Step by Step WCF—Transactions
Step by Step WCF—Transactions   [DataMember]
Step by Step WCF—Transactions   
public int
 Number;
Step by Step WCF—Transactions}


Step by Step WCF—Transactions
Step by Step WCF—Transactions[ServiceContract]
Step by Step WCF—Transactions

interface
 IAccountManager
Step by Step WCF—TransactionsStep by Step WCF—Transactions
Step by Step WCF—Transactions

{

Step by Step WCF—Transactions   [OperationContract]
Step by Step WCF—Transactions   Account[] GetAccounts();
Step by Step WCF—Transactions}


Step by Step WCF—Transactions

我们再来看客户端。客户端使用了一个winform程序来模拟银行的操作:
 Step by Step WCF—Transactions
点击Transfer按钮将会做转帐的操作。在代码上,client端会对第一个帐户创建一个TCP代理类来完成贷款动作。接下来会对第二个帐户创建一个HTTP代理类来完成借款动作。完成转帐动作后会重新获取帐户信息显示到grid中。

Step by Step WCF—Transactionsusing(AccountClient account1 = new AccountClient(TCP
))
Step by Step WCF—Transactions
using(AccountClient account2 = new AccountClient(HTTP

))
Step by Step WCF—TransactionsStep by Step WCF—Transactions
Step by Step WCF—Transactions

{

Step by Step WCF—Transactions   account1.Credit(destinationAccount,amount);
Step by Step WCF—Transactions   account2.Debit(sourceAccount,amount);
Step by Step WCF—Transactions}


Step by Step WCF—Transactions

目前client端没有任何事务控制,也没有错误处理。程序的架构如下图所示:
 Step by Step WCF—Transactions

在没有事务控制的情况下,如果帐户号码是正确的,那么不会出现任何问题。比如我们将100元从帐户123转到456。但是如果帐户输入错误了,那么就会有问题了。比如我们将100元从帐户777转到456。点击Transfer后我们会收到异常(因为程序没有错误处理),不用管这个错误,刷新grid后我们会发现456帐户上多了100元!
 Step by Step WCF—Transactions
接下来我们就加入事务控制吧。

加入事务
为AccountService加入operation behavior:

Step by Step WCF—Transactionsclass
 AccountService : IAccount
Step by Step WCF—TransactionsStep by Step WCF—Transactions
Step by Step WCF—Transactions

{

Step by Step WCF—Transactions    [OperationBehavior(TransactionScopeRequired 
= true
)]
Step by Step WCF—Transactions    
public void Credit(int accountNumber, decimal
 amount)
Step by Step WCF—TransactionsStep by Step WCF—Transactions    
Step by Step WCF—Transactions{……}

Step by Step WCF—Transactions
Step by Step WCF—Transactions    [OperationBehavior(TransactionScopeRequired 
= true)]
Step by Step WCF—Transactions    
public void Debit(int accountNumber, decimal
 amount)
Step by Step WCF—TransactionsStep by Step WCF—Transactions    
Step by Step WCF—Transactions{……}

Step by Step WCF—Transactions}


Step by Step WCF—Transactions
Step by Step WCF—Transactions

为了让事务能传播到服务端,我们需要在服务端加上TransactionFlow的属性。同样也需要在client端的contract定义上加入相同的属性:

Step by Step WCF—Transactions
[ServiceContract]
Step by Step WCF—Transactions
interface

 IAccount
Step by Step WCF—TransactionsStep by Step WCF—Transactions
Step by Step WCF—Transactions

{

Step by Step WCF—Transactions    [OperationContract]
Step by Step WCF—Transactions    [TransactionFlow(TransactionFlowOption.Allowed)]
Step by Step WCF—Transactions    
void Credit(int accountNumber, decimal
 amount);
Step by Step WCF—Transactions
Step by Step WCF—Transactions    [OperationContract]
Step by Step WCF—Transactions    [TransactionFlow(TransactionFlowOption.Allowed)]
Step by Step WCF—Transactions    
void Debit(int accountNumber, decimal
 amount);
Step by Step WCF—Transactions}


Step by Step WCF—Transactions
Step by Step WCF—Transactions

同时还需要在配置文件中对bingding加入允许事务的属性,服务端:

Step by Step WCF—Transactions<services>

Step by Step WCF—Transactions 

<service name = “AccountService”>

Step by Step WCF—Transactions    

<
endpoint
Step by Step WCF—Transactions       
address  = “net.tcp://localhost:8001/AccountService/”


Step by Step WCF—Transactions       binding  

= “netTcpBinding”

Step by Step WCF—Transactions       contract 

= “IAccount”

Step by Step WCF—Transactions    bindingConfiguration

=”TransactionalTCP”

Step by Step WCF—Transactions    

/>

Step by Step WCF—Transactions    

<
endpoint
Step by Step WCF—Transactions       
address  = “http://localhost:8002/AccountService”


Step by Step WCF—Transactions       binding  

= “wsHttpBinding”

Step by Step WCF—Transactions       contract 

= “IAccount”

Step by Step WCF—Transactions    bindingConfiguration

=”TransactionalHTTP”

Step by Step WCF—Transactions    

/>

Step by Step WCF—Transactions 

</service>

Step by Step WCF—Transactions ……
Step by Step WCF—Transactions

</services>

Step by Step WCF—Transactions

<bindings>

Step by Step WCF—Transactions   

<netTcpBinding>

Step by Step WCF—Transactions    

<binding name=”TransactionalTCP” transactionFlow=”true” />

Step by Step WCF—Transactions   

</netTcpBinding>

Step by Step WCF—Transactions   

<wsHttpBinding>

Step by Step WCF—Transactions    

<binding name=”TransactionalHTTP” transactionFlow=”true” />

Step by Step WCF—Transactions   

</wsHttpBinding>

Step by Step WCF—Transactions

</bindings>

客户端:

Step by Step WCF—Transactions<client>

Step by Step WCF—Transactions 

<endpoint name = “TCP”

Step by Step WCF—Transactions    address  

= “net.tcp://localhost:8001/AccountService/”

Step by Step WCF—Transactions    binding  

= “netTcpBinding”

Step by Step WCF—Transactions    contract 

= “IAccount”

Step by Step WCF—Transactions     bindingConfiguration

=”TransactionalTCP”

Step by Step WCF—Transactions 

/>

Step by Step WCF—Transactions 

<endpoint name = “HTTP”

Step by Step WCF—Transactions    address  

= “http://localhost:8002/AccountService/”

Step by Step WCF—Transactions    binding  

= “wsHttpBinding”

Step by Step WCF—Transactions    contract 

= “IAccount”

Step by Step WCF—Transactions     bindingConfiguration

=”TransactionalHTTP”

Step by Step WCF—Transactions 

/>

Step by Step WCF—Transactions……
Step by Step WCF—Transactions

</client>

Step by Step WCF—Transactions

<bindings>

Step by Step WCF—Transactions 

<netTcpBinding>

Step by Step WCF—Transactions  

<binding name=”TransactionalTCP” transactionFlow=”true” />

Step by Step WCF—Transactions 

</netTcpBinding>

Step by Step WCF—Transactions 

<wsHttpBinding>

Step by Step WCF—Transactions  

<binding name=”TransactionalHTTP” transactionFlow=”true” />

Step by Step WCF—Transactions 

</wsHttpBinding>

Step by Step WCF—Transactions

</bindings>

Step by Step WCF—Transactions

对client项目添加对System.Transactions.dll的引用。打开BankClientForm.cs文件,添加using语句:using System.Transactions。
下面,我们将在client端使用transaction scope将它调用的两个服务包到一个事务中:
 Step by Step WCF—Transactions
使用TrasactionScope来包住两个调用:

Step by Step WCF—Transactionsusing(TransactionScope scope = new
 TransactionScope())
Step by Step WCF—Transactions
using (AccountClient account1 = new AccountClient(TCP

))
Step by Step WCF—Transactions
using (AccountClient account2 = new AccountClient(HTTP

))
Step by Step WCF—TransactionsStep by Step WCF—Transactions
Step by Step WCF—Transactions

{

Step by Step WCF—Transactions    account1.Credit(destinationAccount, amount);
Step by Step WCF—Transactions    account2.Debit(sourceAccount, amount);
Step by Step WCF—Transactions    scope.Complete();
Step by Step WCF—Transactions}

重复我们一开始的实验,你会发现帐户不正确时所有操作都会进行回滚。

 

转载于:https://www.cnblogs.com/yunkunyang/archive/2008/04/11/1148703.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/111032.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • Oracle number(m,n)类型的大小和比例 ,解决ORA-01438[通俗易懂]

    Oracle number(m,n)类型的大小和比例 ,解决ORA-01438[通俗易懂]ORA-01438报错超出此列允许精度,一般是number字段出错了,录入的数字精度超过了表允许的精度,可以修改表字段的大小和比例.Oracle表字段类型number来存储数字,与varchar2类型相似.大小的就是总长度为多少位,m代表数字的总位数.比例n代表小数的精度位数,比如说number(5,2)就是整数3位,小数2位,例如123.12.number(0,0)是不加限制.可以随意插入数字…

  • busybox最新版(busybox apk)

    把”busybox-armv6l”重命名为”busybox”;将busybox传入手机的SD卡,打开terminal(Linux,Mac)或cmd(Windows)adbpush~/Desktop/busybox/mnt/sdcard其中的~/Desktop请根据自己的情况替换成正确的路径输入以下命令,为了在/system目录写入文件adbshellsumount-oremount,r…

  • datax(22):任务分配规则

    datax(22):任务分配规则前面学习了一些源码和datax的执行,其中有一个重要的流程任务切分。今天梳理下;一、概述Datax根首先据配置文件,确定好channel的并发数目。然后将整个job分成一个个小的task,然后划分成组。从JobContainer的start()方法开始,进入split()方法,split方法里执行后续所有的切分;二、总体流程切分任务channel数目的确定reader的切分Writer的切分合并配置分配任务三、切分任务JobContainer的split负责将整个jo.

  • linux下安装ffmpeg_linux 当前时间

    linux下安装ffmpeg_linux 当前时间linuxffmpeg安装ffmpeg安装不会吧这都22年了还有人问ffmpeg安装第一步我们先去下载资源包这里是4.4版本wgethttp://www.ffmpeg.org/releases/ffmpeg-4.4.tar.gz第二步解压资源包tar-zxvfffmpeg-4.4.tar.gz第3步创建安装目录我是安装在##我是安装在/www/server/ffmpeg下面mkdirffmpeg第4步返回解压目录cdffmpeg-4.

  • bt3 u盘版安装教程_bt3下载中文版下载

    bt3 u盘版安装教程_bt3下载中文版下载安装bt3u盘版在我们生活中非常常见,使用简单、方便,为了安装bt3u盘版,今天推荐给大家的安装bt3u盘版,操作简单,适用于绝大部分用户使用bt3u盘版下载准备工具Bt3程序U盘电脑第一步首先格式化U盘,选择FAT32方式。打开下载好的文件,将下图圈内的两个文件拷贝到U盘bt3u盘安装电脑图解-1点击【运行】,或者点windows+R,输入“CMD”进入,依次输入以下命令输入“U盘盘符”进入U…

  • 数组按照出现次数排列

    数组按照出现次数排列数组按照出现次数排列

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号