WCF学习笔记之事务编程
WCF学习笔记之事务编程
一:WCF事务设置
事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元;
WCF通过System.ServiceModel.TransactionFlowAttribute特性定义在契约的相应操作方法上;
TransctionFlowOption三个选项:NotAllowed、Allowed、Mandatory不同的事务流转策略;
1:NotAllowed(默认) 客户端的事务不会允许被流转到服务端,服务端也不会试图去接收流入的事务;
2:Allowed 如果客户端的事务在,则被流转到服务端,服务端会试图去接收流入的事务;
3:Mandatory 客户端必须在一个事务中进行服务调用,相应的事务会被流转到服务端。服务端接收到的消息 中必须包含被序列化的事务;

[ServiceContract]
public interface IBankingService
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void Transfer(string fromAccountId, string toAccountId, double amount);
}

由于分布式事务在客户端和服务端之间协调过程依赖于它们之间的相互消息交换,因此在一个单向(One-Way)
操作契约上不允许将应用在上面的TransctionFlowAttribute特性指定为:Allowed 或 Mandatory [P143]
二:事务与绑定的联系
支持事务绑定的除BasicHttpBinding(基于WS-I Basic Profile标准绑定)、NetMsmqBinding(只能采用单工的消息交换)、
MsmqIntegrationBinding(只能采用单工的消息交换) 其它都有事务的流转能力;
支持事务的绑定流转默认也是被关闭,要通过配置或者编程的方式开启该选项;
WCF支持三种不同的事务处理协议:OleTx、WS-AT 1.0、WS-AT 1.1 分别对应于TransactionProtocol中的三个静态只读
属性OleTransactions(与Default默认一样)、WSAtomicTransationOctober2004、WSAtomicTransaction11
NetTcpBinding和NetNamedPipeBinding通过TransactionFlow设置支持事务的开关,TransactionProtocol设置事务协议;
WSHttpBinding、WSDualHttpBinding和WSFedrationHttpBinding支持协议WS-AT 1.0,而WS2007HttpBinding和
WS2007FederationHttpBinding支持协议WS-AT 1.1,它们仅仅只有TransactionFlow 没有TransactionProtocol设置事务协议;

<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="transactionalTcpBinding" transactionFlow="true"
transactionProtocol="WSAtomicTransactionOctober2004">
</binding>
</netTcpBinding> <ws2007HttpBinding>
<binding name="transactionalHttpBinding" transactionFlow="true"></binding>
</ws2007HttpBinding>
</bindings>
<services>
<service name="Service.WithdrawService">
<endpoint binding="customBinding"
bindingConfiguration="transactionalTcpBinding"
contract="Service.Interface.IWithdrawService" />
</service>
<service name="Service.DepositService">
<endpoint binding="customBinding"
bindingConfiguration="transactionalHttpBinding"
contract="Service.Interface.IDepositService" />
</service>
</services>
</system.serviceModel>
</configuration>

三:通过服务(操作)行为控制事务
通过服务契约确定事务流转的策略,通过事务绑定实施事务的流转;通过服务操作设置提交方式及是否自动登记到事务之中
服务操作OperationBehaviorAttribute两个事务管理相关的属性:TransactionAutoComplete 和 TransactionScopeRequired
1:TransactionAutoComplete(默认值Flase) 表示相应操作的执行是否自动纳入一个事务中;
2:TransactionScopeRequired(默认值true) 表示如果执行过程没有抛出异常,完成后将自动提交事务;

public class BankingService : IBankingService
{
[OperationBehavior(TransactionScopeRequired = true)]
public void Transfer(string fromAccountId, string toAccountId, double amount)
{
}
}

四:事务相关的服务行为
ServiceBehaviorAttribute定义的4个与事务相关的属性;
1:TransactionIsolationLevel:事务的隔离级别,默认值为IsolationLevel.Seriallizable;
2:TransactionTimeout 以字符串形式定义事务的超时时限;
3:TransactionAutoCompleteOnSessionClose 在会话正常结束(没有异常)之后是否自动提交或完成开启的事务,默认值False
4:ReleaseServiceInstanceOnTransactionComplete 当事务完成之后是否要将服务实例释放掉,默认值False

[ServiceBehavior(TransactionIsolationLevel=IsolationLevel.ReadCommitted,TransactionTimeout="00:05:00",
TransactionAutoCompleteOnSessionClose=true)]
public class WithdrawService : IWithdrawService
{
[OperationBehavior(TransactionScopeRequired = true)]
public void Withdraw(string accountId, double amount)
{
}
}

注意:若契给服务ServiceBehavior定义的那些属性后 若在其内的操作没有一个设置OperationBehavior(TransactionScopeRequired = true)则会报错;
也可以使用配置方式:

<service name="Service.DepositService" behaviorConfiguration="transactionBehavior">
<endpoint binding="customBinding"
contract="Service.Interface.IDepositService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="transactionBehavior">
<serviceTimeouts transactionTimeout="00:30:00"/>
</behavior>
</serviceBehaviors>
</behaviors>

在事务流转的场景中,流入的事务和目标服务的事务隔离级别必须一致;否则会报异常;客户端调用代码如下;
没有定义事务的级别则采用默认隔离级别Serializable;

using System.ServiceModel;
using Service.Interface;
using System.Transactions; namespace TransactionalService.Client
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<IBankingServicee> channelFactory = new ChannelFactory<IBankingServicee>("bankingservice"))
{
IBankingServicee bankingservice = channelFactory.CreateChannel();
using (TransactionScope transactionScope = new TransactionScope())
{
bankingservice.Transfer();
transactionScope.Complete();
}
}
}
}
}

五:事务实例:
接下来将通过一个简单的事实来介绍一下WCF事务;因为比较简单所以后面直接提供源代码的下载[源代码里面还包括其它信息]:
1:首先是契约的定义:

using System.ServiceModel;
using System.Runtime.Serialization;
using Service.Model;
namespace Service.Interface
{
[ServiceContract(SessionMode=SessionMode.Required)]
public interface ITransactionDemo
{
[OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void AddAccount(AccountModel model); [OperationContract]
[TransactionFlow(TransactionFlowOption.Allowed)]
void OutAccount(AccountModel model);
}
}

2:契约服务的实现内容

using Service.Interface;
using Service.Model;
using System.Data.Common;
using System.ServiceModel;
using Service.DAL;
using System.EnterpriseServices;
using System.Transactions;
namespace Service.ServerDeom
{
[ServiceBehavior(TransactionIsolationLevel = IsolationLevel.Serializable,
TransactionTimeout = "00:05:00",
TransactionAutoCompleteOnSessionClose = true)]
public class TransactionDemoService:ITransactionDemo
{
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = false)]
public void AddAccount(AccountModel model)
{
try
{
AccountDAL.Update(model);
}
catch (Exception ex)
{
throw new FaultException(new FaultReason(ex.Message));
}
} [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = false)]
public void OutAccount(AccountModel model)
{
try
{
AccountDAL.Update(model);
}
catch (Exception ex)
{
throw new FaultException(new FaultReason(ex.Message));
}
}
}
}

3:宿主服务的配置

<bindings>
<netTcpBinding>
<binding name="portSharingBinding" portSharingEnabled="true"></binding>
</netTcpBinding>
<ws2007HttpBinding>
<binding name="transactionalTcpBinding" transactionFlow="true" />
</ws2007HttpBinding>
</bindings>
<services>
<service name="Service.ServerDeom.TransactionDemoService">
<endpoint address="http://127.0.0.1:3724/ExcptDivideService"
binding="ws2007HttpBinding"
bindingConfiguration="transactionalTcpBinding"
contract="Service.Interface.ITransactionDemo"/>
</service>
</services>

4:客户端的配置信息

<endpoint name="TransactionAccount" address="http://127.0.0.1:3724/ExcptDivideService"
binding="ws2007HttpBinding"
contract="Service.Interface.ITransactionDemo"/>

5:客户端的实现代码:

AccountModel model = new AccountModel();
model.Money = 20;
model.Uid = 1;
model.ID = 1; AccountModel OutModel = new AccountModel();
OutModel.ID = 2;
OutModel.Uid = 2;
OutModel.Money = 1; using (ChannelFactory<ITransactionDemo> ChannelFactory = new ChannelFactory<ITransactionDemo>("TransactionAccount"))
{
ITransactionDemo proxy = ChannelFactory.CreateChannel();
using (TransactionScope transactionScope = new TransactionScope())
{
proxy.AddAccount(model);
proxy.OutAccount(OutModel);
transactionScope.Complete();
}
}

本文是学习[WCF全面解析]中有关事务编程的内容;针对WCF事务编程做一个简单的要点记录;
如果,您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】按钮。 因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,[源代码下载]
WCF学习笔记之事务编程的更多相关文章
- 软件测试之loadrunner学习笔记-01事务
loadrunner学习笔记-01事务<转载至网络> 事务又称为Transaction,事务是一个点为了衡量某个action的性能,需要在开始和结束位置插入一个范围,定义这样一个事务. 作 ...
- 孙鑫VC学习笔记:多线程编程
孙鑫VC学习笔记:多线程编程 SkySeraph Dec 11st 2010 HQU Email:zgzhaobo@gmail.com QQ:452728574 Latest Modified ...
- Hadoop学习笔记(7) ——高级编程
Hadoop学习笔记(7) ——高级编程 从前面的学习中,我们了解到了MapReduce整个过程需要经过以下几个步骤: 1.输入(input):将输入数据分成一个个split,并将split进一步拆成 ...
- WCF学习笔记之传输安全
WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...
- WCF 学习笔记之异常处理
WCF 学习笔记之异常处理 1:WCF异常在配置文件 <configuration> <system.serviceModel> <behaviors> <s ...
- WCF 学习笔记之双工实现
WCF 学习笔记之双工实现 其中 Client 和Service为控制台程序 Service.Interface为类库 首先了解契约Interface两个接口 using System.Service ...
- java学习笔记15--多线程编程基础2
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...
- MySQL实战45讲学习笔记:事务隔离级别(第三讲)
一.隔离性与隔离级别 1.事务的特性 原子性 一致性 隔离性 持久性 2.不同事务隔离级别的区别 读未提交:别人改数据的事务尚未提交,我在我的事务中也能读到.读已提交:别人改数据的事务已经提交,我在我 ...
- java学习笔记14--多线程编程基础1
本文地址:http://www.cnblogs.com/archimedes/p/java-study-note14.html,转载请注明源地址. 多线程编程基础 多进程 一个独立程序的每一次运行称为 ...
随机推荐
- Vs2012 构建配置 Lua5.2.3
随着手机游戏client程序员,当然,遇到这样的问题,该游戏已经提交出版.但第二天一早,发现有一个逻辑游戏BUG.怎么办,不严重,在一般情况下,非强制性的更新.假设一个严重BUG,他们将不得不强制更新 ...
- 使用WebBrowser控件时在网页元素上绘制文本或其他自定义内容
原文:使用WebBrowser控件时在网页元素上绘制文本或其他自定义内容 第一次在CNBlogs上发Post是提出一个有关使用WebBrowser控件时对SELECT网页元素操作的疑惑,这个问题至今也 ...
- C# foreach 有用方法具体解释
网上查资料,说foreach 不能改动迭代变量,仅仅能訪问迭代变量.自己理解也不是非常深,通过几个代码进行验证,发现foreach的使用方法还有点特别 验证方法: 1. 迭代变量 为int int[] ...
- VirtualBox更改虚拟机磁盘VDI的大小
流程虚拟机中使用,有时会遇到磁盘大小是不够的,假设一套"动态分配的内存".通过下面的方法来手动扩展磁盘空间. 1.启动CMD命令行.进入VirtualBox安装文件夹.例如 cd ...
- Cocos2d-x3.0 TestCPP文件夹的注意事项
1.不多说了,重力加速度. 2.ActionMangerTest:此Test它是由导演来展示,以获得集体诉讼经理ActionManager类别,操作控制节点. ①CrashTest:破坏demo,毁. ...
- Android EventBus现实 听说你out该
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/40794879.本文出自:[张鸿洋的博客] 1.概述 近期大家面试说常常被问到Ev ...
- PAT 1006. Sign In and Sign Out
#include<iostream> #include<string> using namespace std; int main(){ int cnt;cin>> ...
- CodeSmith 生成代码
使用CodeSmith 生成代码 CodeSmith是一款优秀的代码生成工具.在ORM中,它能帮助我们生成实体类.XML配置文件,从而简化了我们一部分的开发工作.下面简要说说它的基本用法. 1. 打 ...
- Android NDK进入发展
使用互联网有很多javah命令生成一个头文件来完成JNI写,但事实上ADT集成NDK后.点点鼠标就可以了,网上的介绍是非常小懒的方法,在这里,我们主要谈论的懒惰JNI发展. 为ADT组态NDK.请个人 ...
- 使用Vim或Codeblocks格式化代码
在网上的代码,有很多的代码都是丢失缩进的,几行还好,手动改改,多了呢,不敢想象,没有缩进的代码.别说排错,就是阅读都是困难的,还好,有两个常用工具可以轻松的解决问题. (一)Vim(简单方便,可将代码 ...